Dynamically set the height of the lbl text label in ios

Source: Internet
Author: User

Dynamically set the height of the lbl text label in ios

This article will share with you how to dynamically set the height and width of the lbl text label in ios. Three methods are provided for you, and you can freely choose based on your project requirements.

The Code is as follows:

Txtlbl. font = [UIFont boldSystemFontOfSize: 14.0f];

Txtlbl. numberOfLines = 0;

NSString * str = @ "a fang decided to set up the tech special network to leave the TV series and divide the TV series into w. Two Chinese high-tech companies, no longer seen the day I countdown left I said the teacher KFC Frank sajiangdong old father will Federer said a fang decided setting up tech special network and leaving TV series are divided into two Chinese high-tech ones, but the technology is gone, I countdown left me, Li Ke said the teacher KFC Frank sajiangdong father said ";

CGSize size = [str sizeWithFont: txtlbl. font constrainedToSize: CGSizeMake (txtlbl. frame. size. width, MAXFLOAT) lineBreakMode: NSLineBreakByWordWrapping];

// Reset the size of txtlbl based on the calculation result.

[Txtlbl setFrame: CGRectMake (7, 0,310, size. height)];

Txtlbl. text = str;

Method 2:

The Code is as follows:

// IOS7_API _ dynamically determine the Label width and height based on the word count

// Set the Label font HelveticaNeue Courier

UIFont * fnt = [UIFont fontWithName: @ "HelveticaNeue" size: 24366f];

_ NameLabel. font = fnt;

// Obtain the NSString Size Based on the font.

CGSize size = [_ nameLabel. text sizeWithAttributes: [NSDictionary dictionaryWithObjectsAndKeys: fnt, NSFontAttributeName, nil];

// Name H

CGFloat nameH = size. height;

// W of the name

CGFloat nameW = size. width;

_ NameLabel. frame = CGRectMake (0, 0, nameW, nameH );

Method 3:

The Code is as follows:

// W

CGFloat contentW = self. bounds. size. width-_ content. frame. origin. x-kMargin;

// The label font HelveticaNeue Courier

UIFont * fnt = [UIFont fontWithName: @ "HelveticaNeue" size: 18366f];

_ Content. font = fnt;

_ Content. numberOfLines = 0;

_ Content. lineBreakMode = NSLineBreakByWordWrapping;

// Use the following method in iOS7 to replace sizeWithFont: constrainedToSize: lineBreakMode: Method

CGRect tmpRect = [_ content. text boundingRectWithSize: CGSizeMake (contentW, 1000) options: Optional attributes: [NSDictionary attributes: 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.