One detail of IOS computing text width

Source: Internet
Author: User

The designer gives the figure, often the "left side of the button" text to the right of the 5px "such a callout. So we need to calculate the width of the text.
There are two ways of doing this:

    1. label button method of Use sizetofit or

      [label sizeToFit];NSLog(@"label size is %@",[NSValue valueWithCGSize:label.bounds.size]);
    2. Use sizeWithAttributes method

      CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];NSLog(@"size WithAttributes is %@",[NSValue valueWithCGSize:size]);  

How did it end?

Label size is nssize: {96.5, 20.5}
Size Withattributes is nssize: {96.39697265625, 20.287109375}

Visible two return values are different, the second method returns a value that seems accurate, which one should be used?
Let's see what the document says.

    1. About sizeWithAttributes the document

      This method returns fractional sizes; To use a returned size to size of views, you must raise their value to the nearest higher integer using the Ceil function.

    2. About sizeToFit the document

      The call this method is the most appropriate amount of space in the want to resize the current view so it uses.

In the documentation, it is clear that sizeWithAttributes when used, ceil method rounding is required. Instead, sizeToFit the method resizes the current view to a appropriate size.
The key here is to appropriate understand. I say I understand that at least two conditions should be fulfilled:

    1. Do not truncate
      This means that the value returned should not be less than the width/length of the text, otherwise it will be truncated.
    2. Pixel alignment
      All of the view's start and end should be on a physical pixel. Therefore, the @2x screen should return an integer multiple of 0.5, and an integer multiple of 1 should be returned on the @1x screen.

Therefore, the second method should be used, or the result of the first method should be rounded up.
If the result of the first method is used directly, it will cause the other interface pixels to be misaligned and behave strangely.
Imagine how the computer handles "a line segment, where the starting point is 3.452 pixels and the end point is 56.341 pixels" this strange thing.

One detail of IOS computing text width

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.