IOS7 The following dynamically calculates a string size can be used Sizewithfont
-(Cgsize) Sizewithfont: (Uifont *) font
Specific application:
Cgsize statusestrsize = [lcsstring SizeWithFont:string.font];
Or
-(Cgsize) Sizewithfont: (Uifont *) font constrainedtosize: (cgsize) size Linebreakmode: (nslinebreakmode) LineBreakMode;
Specific application:
Cgsize statusestrsize = [lcsstring sizewithfont:[uifont systemfontofsize:17] ConstrainedToSize:theLabel.frame.size Linebreakmode:nslinebreakbycharwrapping];
But after iOS7, Apple abandons this method, can find the same effect method completely, the method is also very simple
Cgsize size = [Self.statusLabel.text sizewithattributes:@{nsfontattributename: [Uifont systemfontofsize:17.0f]}];
Cgsize statusestrsize = Cgsizemake (Ceilf (size.width), Ceilf (Size.Height));
Now the method is more powerful, you can add a dictionary of attributes, such as font, color, underline, background color, etc.
-(Cgsize) Sizewithattributes: (Nsdictionary *) attrs
Specific application:
Cgsize size = [@] This is the Test field "Sizewithattributes:@{nsfontattributename: [Uifont systemfontofsize:18.0f], Nsstrokecolorattributename: [Uicolor greencolor], Nsforegroundcolorattributename:[uicolor GreenColor]}];
Cgsize statusestrsize = Cgsizemake (Ceilf (size.width), Ceilf (Size.Height));
NSLog (@ "%@", Nsstringfromcgsize (statusestrsize));
dynamically calculating the length of a string in iOS