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