iOS dynamically determines label width based on word count

Source: Internet
Author: User

Sometimes when we write a project, we encounter, feedback, and other places, such as the details of the lecture activities need a large display of text,

Because each time the server returns a different size, you need to dynamically adjust the label's width height:

In iOS 6, you can:

-(void) creatlabel{//dynamically determine label width based on word count_namelabel=[[UILabel alloc]init]; _namelabel.frame= CGRectMake ( -, -, -, +); _namelabel.backgroundcolor=[Uicolor Redcolor]; _namelabel.text=@"bodhi tree without trees"; //sets the font of the label Helveticaneue CourierUifont *font = [Uifont fontwithname:@"Helveticaneue"Size24.0f]; _namelabel.font=font; //get nsstring size according to the fontCgsize size =[_namelabel.text sizewithattributes:[nsdictionary dictionarywithobjectsandkeys:font,nsfontattributename, nil]    ]; //the height of the nameCGFloat Nameh =Size.Height; //the width of the nameCGFloat Namew =Size.width; _namelabel.frame= CGRectMake ( -, -, Namew, Nameh); [Self.view Addsubview:_namelabel];}

When iOS 7 is available:

-(void) creattextfield{//dynamically determine the content of a frame based on the contents of the body_content=[[UILabel alloc]init]; _content.frame= CGRectMake ( -, $, self.view.frame.size.width- the, -); _content.backgroundcolor=[Uicolor Redcolor]; _content.clipstobounds=YES; _content.layer.cornerradius=Ten; _content.numberoflines=0; _content.layer.bordercolor=[Uicolor Graycolor].        Cgcolor; _content.layer.borderwidth=0.8; _content.text=@"Today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine, today the weather is fine today, the weather is fine today, today the weather is fine, today the weather is fine and sunny today,  Today the weather is fine and sunny today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine, today the weather is fine and sunny today, Today the weather is fine and sunny today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine, today the weather is fine and sunny today, The weather is fine today."; //widthCGFloat CONTENTW = self.view.bounds.size.width- the; //the font of the label Helveticaneue CourierUifont *fnt = [Uifont fontwithname:@"Helveticaneue"Size18.0f]; _content.font=fnt; //IOS7 replaces SizeWithFont:constrainedToSize:lineBreakMode in obsolete iOS6 using the following methods:CGRect Tmprect= [_content.text boundingrectwithsize:cgsizemake (CONTENTW, +) Options:nsstringdrawinguseslinefragmentorigin attributes:[nsdictionary dictionarywithobjectsandkeys:fnt,    Nsfontattributename, nil] context:nil]; //height HCGFloat Contenth =TmpRect.size.height; NSLog (@"adjusted display width:%f, display height:%f", Contentw,contenth); _content.frame= CGRectMake ( -, $, Contentw,contenth); [Self.view addsubview:_content];}

This can be a good solution to this problem!

iOS dynamically determines label width based on word count

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.