Two methods:
-(Cgsize) Sizewithattributes: (Nullable nsdictionary<nsstring *, id> *) attrs ns_available (10_0, 7_0);
-(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: (Nullable Nsdictionary<nsstring *, id> *) attributes Context: (Nullable Nsstringdrawingcontext *) Context ns_available (10_11 , 7_0);
EG1:
-(void) viewdidload {[Super viewdidload]; NSString *str = @ "The original yarn Yang chose to join the AV industry for their own stable work, and the choice of sod as their owner." In the November 25, 2008 Sabra magazine in the Prian series of the column "Saorichewbe" in the public to join the AV.
";
Set according to what font size to calculate, you can add other attributes nsdictionary *attrs = @{nsfontattributename: [Uifont systemfontofsize:25]};
Label1 Uilabel *label1 = [[Uilabel alloc]init];
Label1.backgroundcolor = [Uicolor Yellowcolor];
Cgsize size1 = [str sizewithattributes:attrs];//Method 1, calculated size, but only one row label1.frame = (cgrect) {{50,50},size1};
Label1.Text = str;
[Self.view Addsubview:label1];
Label2 Uilabel *label2 = [[Uilabel alloc]init];
Label2.backgroundcolor = [Uicolor Purplecolor];
Cgsize size2 = Cgsizemake (maxfloat)//Give a limit size, set width 200, height variable cgrect rect2 = [Str boundingrectwithsize:size2
Options:nsstringdrawinguseslinefragmentorigin Attributes:attrs context:nil];//Method 2 calculates the size, but restricts cgfloat label2_h by size2 = Rect2.size. Height;
Label2.frame = CGRectMake (M, Size2.width, Label2_h);
[Self.view Addsubview:label2];
Label2.Text = str;
Label2.numberoflines = 0; Label1 and Label2 do not set the font size, so you will feel that the label has a lot of white space, see Label3//label3 set the font size, because the default font size is less than 25, so Label1 and Label2 are blank,
Now set Label3 font and calculation based on the same size, see the effect of uilabel *label3 = [[Uilabel alloc]init];
Label3.backgroundcolor = [Uicolor Graycolor];
Cgsize size3 = Cgsizemake (maxfloat); CGRect rect3 = [str boundingrectwithsize:size2 options:nsstringdrawinguseslinefragmen
Torigin Attributes:attrs context:nil];//Method 2 CGFloat label3_h = rect3.size.height;
Label3.frame = CGRectMake (Size3.width, label3_h);
[Self.view Addsubview:label3];
Label3.font = [Uifont systemfontofsize:25];//compared to label2 more of this code label3.text = str;
Label3.numberoflines = 0; }