Get the height and Tableviewcell of the label Chinese text in iOS

Source: Internet
Author: User

1.NSString there is a way to get the actual occupied height and width of the string. Can be used to UITableViewCell adaptive
Before IOS7, use the below

-(Cgsize) Sizewithfont: (Uifont *) font constrainedtosize: (cgsize) size Linebreakmode: (nslinebreakmode) LineBreakMode;

is used to get the text content within the specified size, to figure out the actual width and height required. Such as

// This is the width and height required to get the string within the specified size (the width is more than 175, the line wraps). cgsize maxSize = cgsizemake (175= [@ " This is the data of the test " Sizewithfont:[uifont Systemfontofsize:constrainedtosize:maxsize linebreakmode:nslinebreakbywordwrapping]; // the height of the string CGFloat labelheight = size.height;

in IOS7 and later versions, the Sizewithfont method is obsolete . Use

-(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: (Nsdictionary *) Attributes context: (Nsstringdrawingcontext *) context
Replace.

NSString *strtest =@"This is a test data"; Nsmutableparagraphstyle*paragraphstyle =[[Nsmutableparagraphstyle alloc]init];p aragraphstyle.linebreakmode=nslinebreakbywordwrapping; Nsdictionary*attributes = @{nsfontattributename:[uifont Systemfontofsize: -], nsparagraphstyleattributename:paragraphstyle}; Cgsize labelsize= [Strtest Boundingrectwithsize:cgsizemake (207, maxfloat) options:nsstringdrawinguseslinefragmentorigin attributes:attributes context:nil].size; NSLog (@"---------%f", labelsize.height);

These can be applied to the highly adaptive uitableviewcell, if there is a label on the custom cell.

2. Calculate the actual number of rows in the Uilabel

Uilabel Although you can set an unlimited number of rows by using the NumberOfLines = 0 property, but when there are multiple rows, you need to know the actual number of lines of the font, you can use:

UILabel *mylabel =[[UILabel alloc] Init];mylabel.frame= CGRectMake ( -,5,214,0.5); NSString*strtest =@"This is a test data"; Cgsize maxSize= Cgsizemake (screenwidth-175, maxfloat); Uifont*font = [Uifont systemfontofsize: -]; Cgsize size=[strtest sizewithfont:font constrainedtosize:maxsize linebreakmode:nslinebreakbywordwrapping];//Size.Height is the actual height of the stringCGFloat labelnum = size.height/mylabel.font.lineheight;

Labelnum is the actual number of rows

Get the height and Tableviewcell of the label Chinese text in iOS

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.