iOS dynamically set the height of the LBL text label

Source: Internet
Author: User

This article to share is the iOS dynamically set LBL text label height width of the method, a total of 3 methods, the small partners according to their own project needs free choice.

The code is as follows:

Txtlbl.font = [Uifont boldsystemfontofsize:14.0f];

Txtlbl.numberoflines = 0;

NSString *str = @ " Albania decides to set up science and technology network to leave TV series two mainland high tech got lost. Day I countdown left I said the teacher Kentucky Franksa Jiangdong Elders will Roger Federer said Albania decided to set up a network of science and technology to leave the TV show two of the mainland High-tech has disappeared day I countdown to leave my branch said the teacher, Kentucky Franksa Jiangdong elders will Federer said ";

Cgsize size = [str sizeWithFont:txtlbl.font constrainedtosize:cgsizemake (txtlbl.frame.size.width, Maxfloat) Linebreakmode:nslinebreakbywordwrapping];

Reset the size of the TXTLBL based on the results

[Txtlbl Setframe:cgrectmake (7, 0, 310, size.height)];

Txtlbl.text = str;

Method Two:

The code is as follows:

Ios7_api_ determines the label width according to the word Word character dynamic

Sets the font for the label Helveticaneue Courier

Uifont *fnt = [Uifont fontwithname:@ "Helveticaneue" size:24.0f];

_namelabel.font = FNT;

According to the font to get nsstring size

Cgsize size = [_namelabel.text sizewithattributes:[nsdictionary dictionarywithobjectsandkeys:fnt, Nsfontattributename, Nil]];

The h of the name

CGFloat Nameh = size.height;

Name of the W

CGFloat Namew = size.width;

_namelabel.frame = CGRectMake (0, 0, Namew,nameh);

Method Three:

The code is as follows:

Width W

CGFloat contentw = self.bounds.size.width-_content.frame.origin.x-kmargin;

Label's font helveticaneue Courier

Uifont *fnt = [Uifont fontwithname:@ "Helveticaneue" size:18.0f];

_content.font = FNT;

_content.numberoflines = 0;

_content.linebreakmode = nslinebreakbywordwrapping;

IOS7 replaces the SizeWithFont:constrainedToSize:lineBreakMode in obsolete iOS6 with the following methods: Method

CGRect tmprect = [_content.text boundingrectwithsize:cgsizemake (CONTENTW, 1000) options: Nsstringdrawinguseslinefragmentorigin Attributes:[nsdictionary Dictionarywithobjectsandkeys:fnt, Nsfontattributename, nil] context:nil];

Height h

CGFloat contenth = tmpRect.size.height;

NSLog (@ "Adjusted display width:%f, display height:%f" contentw,contenth);

_content.frame = CGRectMake (0, 0, contentw,contenth);

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.