dynamically calculating the length of a string in iOS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.