About the height of dynamic computing text content under iOS, after looking at and searching on the web, now see the following methods:
1.
Gets the size of the string IOS6
-(Cgsize) Getstringrect_: (nsstring*) astring {
Cgsize size;
Uifont *namefont=[uifont fontwithname:@ "Helvetica" size:13];
Size=[astring Sizewithfont:namefont constrainedtosize:cgsizemake (237, linebreakmode:nslinebreakbycharwrapping) ];
return size;
}
Gets the size of the string iOS7
-(float) heightforstring: (NSString *) value andwidth: (float) Width withfontsize: (cgfloat) fontsize{
Gets the properties of the current text
nsattributedstring *attrstr = [[Nsattributedstring alloc] initwithstring:value];
Nsrange range = nsmakerange (0, attrstr.length);
Gets the attribute dictionary for the segment attributedstring
Nsdictionary *dic = [Attrstr attributesatindex:0 effectiverange:&range];
Calculate the size of text
Cgsize SizeToFit = [Value boundingrectwithsize:cgsizemake (width, Cgfloat_max)//for calculating the rectangular block occupied by text drawing
Options:nsstringdrawinguseslinefragmentorigin | Nsstringdrawingusesfontleading//Additional options when text is drawn
Attributes:dic//Text properties
Context:nil
].size; Context contexts. Includes information such as how to adjust word spacing and zoom. The object contains information that will be used for text drawing. This parameter can be nil
return sizetofit.height;
}
The above method also relies on the 200来 of Cgsizemake (237,200) to set the height of the label, and regardless of how many rows are formed within the valid width range, the height of the calculated label is just the height of the row.
The following methods remain the same:
-(Cgsize) Boundingrectwithsize: (cgsize) size
{
Nsdictionary *attribute = @{nsfontattributename:self.font};
Cgsize retsize = [Self.text boundingrectwithsize:size
Options:\
Nsstringdrawingtruncateslastvisibleline |
Nsstringdrawinguseslinefragmentorigin |
Nsstringdrawingusesfontleading
Attributes:attribute
Context:nil
].size;
return retsize;
}
The following code is for you to test:
String nsstring *str = @ "At sunset, brew a cup of Camellia, listen to a song artistic conception empty Far" Zen ", mind with this nature, indulge in mysterious fantasy. As if I was that shuttle in luxuriantly forest in a bird, sometimes circling shuttle, sometimes sing; as if I was that gurgling pouring in a mountain stream of a clear spring, ripples light, Hao Miao long flow; as if I was Nagan in the heaven and earth between a hill, stalwart towering, calmly stretching. I do not believe in Buddha, just like the ethereal sound of the Vatican, and the mercy of the Buddhist meditation, in the Qing Huan, calmly quiet, comfortable Enron. has been longing to go into the green Mountains, blue water, understanding the beauty of the landscape, appreciate the rise and fall of vegetation, and listen to the sound of Huang Tian thick earth, to explore the natural wok too of the universe. Into the landscape, also out of the noise, to the body and mind to cool, to the spirit to precipitate, to the soul to sublimate. "; Initialize label UILabel *label = [UILabel new]; Label.backgroundcolor = [Uicolor whitecolor]; [Self.view Addsubview:label]; Label gets the string label.text = str; Label gets the font label.font = [Uifont fontwithname:nil size:18]; Calculates the size cgsize size = [Label boundingrectwithsize:cgsizemake (0)] based on the obtained string and the font that the label needs. Set infinite line break label.numberoflines = 0; Set the label frame label.frame = CGRectMake (0.0f, 50.0f, Size.width, size.height);
Finally, careful study, Sizewithfont this method seems to be really aimed at uilabel, if the use of Uilabel to show that its calculated height and the actual height is consistent. However, it is important to note that the Uilabel text content and the width of the Uilabel is almost coincident, which makes people feel that the text is too much, you can consider setting its contentinset and so on to deal with, but this is another problem. And the Uitextview is not too side, and the line spacing is large, suitable for display. Therefore, the following methods can be used to solve:
Declaration, definition method:
-(float) heightforstring: (NSString *) value andwidth: (float) Width withfontsize: (cgfloat) fontsize{
Use Uitextview's Sizethatfits method to calculate the height of a string and then use it for UILabel
Uitextview *detailtextview = [[Uitextview alloc]initwithframe:cgrectmake (0, 0, width, 0)];
Detailtextview.font = [Uifont systemfontofsize:fontsize];
Detailtextview.text = value;
Cgsize desize = [Detailtextview sizethatfits:cgsizemake (Width,cgfloat_max)];
return desize.height;
}
Use calculation:
String nsstring *context = @ "At sunset, brew a cup of Camellia, listen to a song artistic conception empty Far" Zen ", mind with this nature, indulge in mysterious fantasy. As if I was that shuttle in luxuriantly forest in a bird, sometimes circling shuttle, sometimes sing; as if I was that gurgling pouring in a mountain stream of a clear spring, ripples light, Hao Miao long flow; as if I was Nagan in the heaven and earth between a hill, stalwart towering, calmly stretching. I do not believe in Buddha, just like the ethereal sound of the Vatican, and the mercy of the Buddhist meditation, in the Qing Huan, calmly quiet, comfortable Enron. has been longing to go into the green Mountains, blue water, understanding the beauty of the landscape, appreciate the rise and fall of vegetation, and listen to the sound of Huang Tian thick earth, to explore the natural wok too of the universe. Into the landscape, also out of the noise, to the body and mind to cool, to the spirit to precipitate, to the soul to sublimate. ";
CGFloat conh = [self heightforstring:context andwidth:200 withfontsize:14];
UILabel *contentlabel = [[UILabel alloc]init];
Contentlabel.textalignment = Nstextalignmentcenter;
Contentlabel.text = ConText;
Contentlabel.numberoflines = 0;
[Contentlabel setframe:cgrectmake (0, 0, Conh)];
[Self.view Addsubview:contentlabel];
IOS dynamically calculates the height of text content