Uilabel height adjustment precautions in iOS7.0

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/k12104/article/details/33731833

http://herkuang.info/blog/2013/12/31/ios7%E4%B8%ADuilabel%E9%AB%98%E5%BA%A6%E8%B0%83%E6%95%B4%E6%B3%A8%E6%84% 8f%e4%ba%8b%e9%a1%b9/

After upgrading to IOS7, my "mnemonic assistant" has been having problems with Uilabel dislocation:

My label is with-(cgsize) Sizewithfont: (Uifont *) font constrainedtosize: (cgsize) size Linebreakmode: (Nslinebreakmode) Linebreakmode to calculate, but it seems that the calculation is not very correct.

In fact Uilabel frame is the size of the red box, but when the width is not enough, do not know what triggered the bug, the label when the text will be squeezed down. I'm not sure what the problem is, but if you increase the width of the Uilabel, it will appear normal.

After following the code to find, under IOS7,-(Cgsize) Sizewithfont: (Uifont *) font constrainedtosize: (cgsize) size Linebreakmode: ( Nslinebreakmode) Linebreakmode The value returned by this function is a decimal, and after the frame is set to Uilabel, the width of the uilabel is less than the width of the text to be drawn. It will cause the problem above.

As you can see in the official documentation,-(cgsize) Sizewithfont: (Uifont *) font constrainedtosize: (cgsize) size Linebreakmode: (Nslinebreakmode) Linebreakmode This function has been deprecated in iOS7 and replaced by BoundingRectWithSize:options:attributes:context: this function. In fact, the results of these two functions in iOS7 are consistent, with decimals. BoundingRectWithSize:options:attributes:context: In the documentation, you can see this sentence:

This method returns fractional sizes (in the size component of the returned CGRect); To use a returned size to size of views, you must use raise its value to the nearest higher integer using the Ceil function.

In other words, the calculated size needs to be rounded with the ceil function.

In IOS7, the following steps are required to correctly calculate the height Uilabel may occupy:

Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc]init];
Paragraphstyle.linebreakmode = nslinebreakbywordwrapping;
Nsdictionary *attributes = @{nsfontattributename:somefont, NSParagraphStyleAttributeName:paragraphStyle.copy};

Labelsize = [Sometext boundingrectwithsize:cgsizemake (207, 999) options:nsstringdrawinguseslinefragmentorigin Attributes:attributes context:nil].size;
/*
This method returns fractional sizes (in the size component of the returned CGRect); To use a returned size to size of views, you must use raise its value to the nearest higher integer using the Ceil function.
*/
Labelsize.height = Ceil (labelsize.height);
Labelsize.width = Ceil (labelsize.width);

Uilabel height adjustment precautions in iOS7.0

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.