An error occurred while calculating the label width (height) for ios on iPhone 5s and iPhone 6.

Source: Internet
Author: User

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:

 

Related Article

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.