Transferred from: http://www.cnblogs.com/spiritstudio/archive/2011/11/17/2252074.html
When you use Uilabel to store strings, you often need to get the long-width data for the label, which is a list of some common computational methods.
1. Get the width to get the length of the string without wrapping the row line
Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (Maxfloat, 30)];
Note: The width of size should be set to maxfloat if it is desired.
2. Gets the height that gets the actual height required for the string to wrap within the specified size (the width of the label is more than the line).
Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:uilinebreakmodewordwrap];
Note: The height of size should be set to maxfloat if you want to get high.
3. When actually programming, sometimes you need to calculate the position of the last character of a piece of text, and then add a picture or other control (such as the info icon), the following code is the method to calculate the position of the last character in the label.
Cgsize sz = [Label.text sizeWithFont:label.font constrainedtosize:cgsizemake (Maxfloat, 40)];
Cgsize linessz = [Label.text sizeWithFont:label.font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:uilinebreakmodewordwrap];
if (sz.width <= linessz.width)//Determine if line is broken
{
Lastpoint = Cgpointmake (label.frame.origin.x + sz.width, LABEL.FRAME.ORIGIN.Y);
}
Else
{
Lastpoint = Cgpointmake (label.frame.origin.x + (int) sz.width% (int) linessz.width,linessz.height-sz.height);
}
Article two, transfer from: http://www.cnblogs.com/heri/archive/2013/03/18/2965817.html
In the case of most attributes, when dynamic Data binding is given to Uilabel, it is often necessary to dynamically adjust the width or height of the Uilabel depending on how much the string is.
Here are two things to consider:
1, Uilabel width unchanged, according to the number of fonts, automatically adjust the height of uilabel, and fold line display.
The code is as follows:
- UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, 10, 200, 20)];
- Label.font = [Uifont boldsystemfontofsize:20.0f]; font size of//uilabel
- Label.numberoflines = 0; //must define this property, otherwise Uilabel will not wrap.
- Label.textcolor = [Uicolor Whitecolor];
- Label.textalignment = Nstextalignmentleft; //Text alignment
- [Label Setbackgroundcolor:[uicolor Redcolor];
- The width is constant, the height of the label is calculated based on how much the word
- NSString *str = @"Can change this content to test, the width is unchanged, the height is automatically adjusted according to the content";
- Cgsize size = [str sizeWithFont:label.font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:nslinebreakbywordwrapping];
- Reset the size of the Uilabel based on the calculation results
- [Label Setframe:cgrectmake (0, ten, $, size.height)];
- Label.text = str;
- [Self.view Addsubview:label];
- [Label release];
2, Uilabel height unchanged, according to the font number, automatically adjust the width of the Uilabel, and fold the line display
The code is as follows:
- UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, 10, 20, 20)];
- Label.font = [Uifont boldsystemfontofsize:20.0f]; font size of//uilabel
- Label.numberoflines = 0; //must define this property, otherwise Uilabel will not wrap.
- Label.textcolor = [Uicolor Whitecolor];
- Label.textalignment = Nstextalignmentleft; //Text alignment
- [Label Setbackgroundcolor:[uicolor Redcolor];
- The height of a fixed line, according to the number of words to calculate the width of the label
- NSString *str = @"Height invariant gets width, gets the length of the string that is required to display the line lines without folding";
- Cgsize size = [str sizeWithFont:label.font constrainedtosize:cgsizemake (maxfloat, label.frame.size.height)];
- NSLog (@"size.width=%f, size.height=%f", Size.width, Size.Height);
- Reset the size of the Uilabel based on the calculation results
- [Label Setframe:cgrectmake (0, ten, Size.width, 20)];
- Label.text = str;
- [Self.view Addsubview:label];
- [Label release];
In either case, the core code is the code at size, and the corresponding size is set to Maxfloat
IPhone: Get the height and width of Uilabel dynamically