When you use Uilabel to store strings, you often need to get the long width data of the label, this article lists some of the commonly used calculation methods.
1. Get the width, get the length required for the string to be displayed in a single line
Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (Maxfloat, 30)];
Note: The width of size should be set to maxfloat if desired.
2. Gets the height, gets the actual height required for the string to wrap within the specified size, which is longer than the label width.
Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:uilinebreakmodewordwrap];
Note: If you want to get a height, the height of size should be set to Maxfloat.
3. In actual programming, it is sometimes necessary 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), and the following code is a way 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)//Judge if the 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);
}
ios7.0 after the use of:
CGFloat width1=[(NSString *) ob1 Sizewithfont:[uifont systemfontofsize:16] Constrainedtosize:cgsizemake (1000, Fontheight)].width;
Turn
CGFloat width1=[(NSString *) Ob1 boundingrectwithsize:cgsizemake (1000, fontheight) options: Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont SystemFontOfSize:16]} Context:nil] . size.width;
Boundingrectwithsize:cgsize corresponds to Constrainedtosize:cgsize
Attributes:@{nsfontattributename: [ uifont ]} corresponds to sizewithfont[ Uifont ]
Options:nsstringdrawinguseslinefragmentorigin Default Photo Fill
Context:nil Default Photo Fill
Turn from: http://www.cnblogs.com/spiritstudio/archive/2011/11/17/2252074.html