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