When using uilabel to store strings, you often need to obtain the long and wide data of the label. This article lists some common calculation methods.
1. Get the width and get the length required when a single line of string is displayed
Cgsize titlesize = [astring sizewithfont: font constrainedtosize: cgsizemake (maxfloat, 30)];
Note: If you want the width, the width of the size should be set to maxfloat.
2. Get the height. Get the actual height of the string within the specified size (if the width exceeds the label width, wrap the line.
Cgsize titlesize = [astring sizewithfont: font constrainedtosize: cgsizemake (Label. Frame. Size. Width, maxfloat) linebreakmode: uilinebreakmodewordwrap];
Note: If you want to get the height, set the size height to maxfloat.
3. in actual programming, it is sometimes necessary to calculate the position of the last character of a text segment, and add an image or other controls (such as the info icon) after it ), the following code calculates 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 whether to fold
{
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 );
}
IPhone: Dynamically Retrieve the height and width of uilabel