Uilabel the label width based on text, font size, and adaptive height

Source: Internet
Author: User

Download address github source or demo download

Want to get all the fonts, as follows:

Gets all the font names
    nsarray *familynames = [Uifont familynames];
    NSLog (@ "All font names--%@", familynames);

One single line of text

For single-line text, use the Sizewithattributes method directly, as follows:

-(void) viewdidload {
    [super viewdidload];
    Gets all the font names
    nsarray *familynames = [Uifont familynames];
    NSLog (@ "All font names--%@", familynames);
    Uilabel *lbl_text = [[Uilabel alloc]init];
    Lbl_text.backgroundcolor = [Uicolor greencolor];
    Lbl_text.text = @ "We are a family";
    Sets the font for the label Helveticaneue  Courier
    uifont *fnt = [Uifont fontwithname:@ "Courier New" size:24.0f];
    Lbl_text.font = fnt;
    According to the font to get nsstring size
    cgsize size = [Lbl_text.text sizewithattributes:[nsdictionary Dictionarywithobjectsandkeys : Fnt,nsfontattributename,nil]];
    H
    cgfloat Nameh = Size.Height of the name;
    The name of the W
    cgfloat namew = size.width;
    Lbl_text.frame = CGRectMake (100,100, Namew,nameh);
    [Self.view Addsubview:lbl_text];
}

As shown in figure:


Two or more lines of text

First, the numberoflines of Uilabel is set to 0, followed by-(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: (Nsdictionary *) The attributes context: (Nsstringdrawingcontext *) to compute cgsize with the following code:

-(void) viewdidload {
    [super viewdidload];
    Gets all the font names
    nsarray *familynames = [Uifont familynames];
    NSLog (@ "All font names--%@", familynames);
    [Self setmultiline];
}

-(void) setmultiline
{
    Uilabel *lbl_text = [Uilabel new];
    Lbl_text.font = [Uifont systemfontofsize:14];
    Lbl_text.text = @ "text in linguistics, the visual form of written language, the ancient single word" Wen ", the combination of words called" word ", now together called" words ", the basic individual of the text is called" word ". In daily life, "writing" can also refer to written language, language, articles, words and so on. Visual symbol form, break the time and space limit of spoken language. such as Chinese characters, Latin alphabet. ";
    Lbl_text.backgroundcolor = [Uicolor greencolor];
    Lbl_text.numberoflines = 0;//Multiple lines display, compute height
    lbl_text.textcolor = [Uicolor lightgraycolor];
    Cgsize lblsize = [Lbl_text.text boundingrectwithsize:cgsizemake ([UIScreen mainscreen].bounds.size.width-20, MAXFLOAT ) Options:nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont SystemFontOfSize:14]} Context:nil].size;
    Lbl_text.frame = CGRectMake (Ten, Lblsize.width, lblsize.height);
    [Self.view Addsubview:lbl_text];
}

As shown in figure:




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.