An error occurred while calculating the label width (height) for ios on iPhone 5s and iPhone 6.
When using Label today, we need to calculate the UILabel height. We found that the height displayed on 4, 4s is normal, and an error is displayed on iPhone 5s and iPhone 6.
The method is as follows:
NSString * teacherComment = @ comments are written to students. Therefore, comments must be written in English that students can understand, the vocabulary and syntax used cannot be too high or below the current level of the students. It must be in line with the actual level of the students and the individual psychology of the students ...; float commentHeight = 0.0; if ([teacherComment isEqual: @] | teacherComment = nil | [[NSNull null] isEqual: teacherComment]) {commentHeight = BASIC_LABEL_HEIGHT ;} else {CGSize commentRect = [teacherComment sizeWithFont: [UIFont fontWithName: Large size: Large] rows: CGSizeMake (180, CGFLOAT_MAX) lineBreakMode: Large]; if (commentRect. height> BASIC_LABEL_HEIGHT) {commentHeight = commentRect. height;} else {commentHeight = BASIC_LABEL_HEIGHT ;}}
The code is displayed normally on iPhone 4 or iPhone 4S, and the system is iOS 7 and iOS 8. However, after running on the iPhone 5s and iPhone 6 real machines, it is found that the display is less abnormal and there are some errors.
This problem is very strange. Record it here, search online, and use the following method:
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 determine the system version when using it, so note that this method is used if it is ios7, and ios6 uses the above method.
Error: