iOS calculates label width (height) error on iphone5s, iphone6

Source: Internet
Author: User

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

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.