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