Add a method to NSString by Category:
1. Calculate the size of a string based on string style, string length
/**
* @param font string style
* @param maxWidth Specify string length
*/
-(Cgsize) Sizewithfont: (Uifont *) Font maxWidth: (cgfloat) maxwidth{//Get Text StyleNsmutabledictionary *attrs =[Nsmutabledictionary dictionary]; Attrs[nsfontattributename]=font; //calculate the size of text based on text style//Maximum text widthCgsize maxSize =Cgsizemake (MaxWidth, maxfloat); //Nsstringdrawinguseslinefragmentorigin, start from scratch return[self boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attrs Context:nil] . Size;}
2. Calculate the size of a string based on a string style
/**
* @param font string style
*/
-(Cgsize) Sizewithfont: (Uifont *) font{ return [self Sizewithfont:font maxwidth:maxfloat];}
IOS calculates the size of a string based on string style, string length