Today, when using the label, we need to calculate the height of the Uilabel, find that it is normal on the 4,4s, show error on iphone5s, iphone6
The method used is:
NSString *teachercomment = @ "Reviews are written for students to read, so on the one hand, the use of students can read English to write, the use of vocabulary and grammar can not be too high or lower than the current level of students, to meet the actual level of students, in line with the students ' personality psychology ..."; float commentheight = 0.0; if ([Teachercomment isequal:@ ""]| | teachercomment==nil| | [[[NSNull NULL] isequal:teachercomment]) { commentheight = basic_label_height; } else{ cgsize commentrect = [teachercomment sizewithfont:[uifont fontwithname:title_font Size:FONT_SIZE_FOURTEEN] Constrainedtosize:cgsizemake (Cgfloat_max) linebreakmode:nslinebreakbycharwrapping]; if (Commentrect.height > Basic_label_height) { commentheight = commentrect.height; } else { commentheight = basic_label_height; } }
This code appears normal on IPhone4 or iphone4s, and the system is IOS7,IOS8 system, respectively. However, after running on the iphone5s and Iphone6, it is found that the display is not normal and there are some errors.
This question is very strange, in this record, Internet search, use the method can:
Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc]init]; Paragraphstyle.linebreakmode = nslinebreakbywordwrapping; Nsdictionary *attributes = @{nsfontattributename:[uifont Fontwithname:title_font Size:FONT_SIZE_FOURTEEN], NSParagraphStyleAttributeName:paragraphStyle.copy}; Cgsize labelsize = [Teachercommentlabel.text boundingrectwithsize:cgsizemake (180.0f, Cgfloat_max) options: Nsstringdrawinguseslinefragmentorigin attributes:attributes context:nil].size; Labelsize.height = Ceil (labelsize.height); Labelsize.width = Ceil (labelsize.width); if (Labelsize.height > Basic_label_height) { commentheight = labelsize.height; } else { commentheight = basic_label_height; }
You also need to judge the system version when you use it, so be aware that if you use this method with iOS7, IOS6 uses one of the above methods.
Error:
If you have any good method, please leave a message to inform, thank you ...
Extended reading: www.wahenzan.com
iOS calculates label width (height) error on iphone5s, iphone6