iOS Learning-12. nsmutableattributedstring Calculating Height

Source: Internet
Author: User

To calculate the nsmutableattributedstring height, you must have two properties

-(void) test{UILabel*label = [[UILabel alloc]initwithframe:cgrectmake ( -, -, -, -)]; Label.backgroundcolor=[Uicolor Redcolor]; //May branchLabel.numberoflines =0; NSString*str =@"Modifying the data is obviously a task that belongs to the model layer. Model should expose an API for operations such as delete or reorder, and then we can invoke it in the data source method. This allows the controller to act as a coordinator between the view and model without needing to know the implementation details of the model layer. And with the added benefit, the model logic becomes easier to test because it is no longer mixed up with view controllers's tasks."; //string with variable propertyNsmutableattributedstring *STR1 =[[Nsmutableattributedstring alloc]initwithstring:str]; //Variable RangeNsrange range = {0, [str1 length]}; //the words in the range are underlined[str1 addattribute:nsunderlinestyleattributename value:[nsnumber Numberwithinteger:nsunderlinestylesingle]    Range:range];    [str1 addattribute:nsfontattributename Value:label.font Range:range]; //the font color in the range is green[str1 addattribute:nsforegroundcolorattributename Value:[uicolor Greencolor] Range:nsmakerange (0, -)]; //Calculate the height of a stringCgsize attsize = [str1 boundingrectwithsize:cgsizemake ( -, maxfloat) Options:nsstringdrawinguseslinefragmentorigin |nsstringdrawingusesfontleading context:nil].size; Label.attributedtext=str1; Label.frame= CGRectMake ( -, -, Attsize.width, attsize.height); [Self.view Addsubview:label];}

iOS Learning-12. nsmutableattributedstring Calculating Height

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.