iOS development--dynamically set label size based on label content

Source: Internet
Author: User

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; }





Related Article

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.