Several methods and contrast _ios of IOS acquiring label height

Source: Internet
Author: User

Introduced

When you set the Frame height of a uilabel, you cannot simply set it to a font font size . Otherwise, a portion of the font will be cropped off. Because Uilabel in different font settings, the height of the Frame requirements are not the same, in most cases, the height of the font is set higher.

First, Sizethatfits

view the sizeThatFits method used.

Return the ' best ' size to fit given size. Does not actually resize view. Default is return existing view size
-(cgsize) Sizethatfits: (cgsize) size;

Example:

Uilabel *testlabel = [[Uilabel alloc] init];
Testlabel.font = [Uifont systemfontofsize:30];
Testlabel.text = @ "Today is a fine day";
Cgsize size = [TestLabel sizethatfits:cgsizemake];
NSLog (@ "size =%@", nsstringfromcgsize (size));

Output:size = {246.33333333333334, 36}

Second, SizeToFit

view the sizeToFit method used.

Note: sizeToFit Will change view the original bounds , but sizeThatFits not.

Calls Sizethatfits:with current view bounds and changes bounds size.
-(void) SizeToFit;

Example

Uilabel *testlabel = [[Uilabel alloc] init];
Testlabel.font = [Uifont systemfontofsize:30];
Testlabel.text = @ "Today is a fine day";
[TestLabel SizeToFit];
NSLog (@ "size =%@", Nsstringfromcgsize (testLabel.frame.size));

Output:size = {246.33333333333334, 36}

Third, Sizewithattributes

NSString the sizeWithAttributes method used.

-(Cgsize) Sizewithattributes: (Nullable nsdictionary<nsstring *, id> *) attrs ns_available (10_0, 7_0);

Example

NSString *text = @ "is a fine Day";
Uifont *font = [Uifont systemfontofsize:30];
Cgsize size = [text sizewithattributes:@{
           Nsfontattributename:font
           }];
NSLog (@ "size =%@", nsstringfromcgsize (size));

Output:size = {246.3134765625, 35.80078125}

Four, Boundingrectwithsize

NSString the boundingRectWithSize method used.

Note:all of the following methods would default to drawing on a baseline, limiting drawing to a single line.
To correctly draw and size multi-line text, pass Nsstringdrawinguseslinefragmentorigin in the options parameter.
-(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: (Nullable Nsdictionary<nsstring *, id> *) attributes Context: (Nullable Nsstringdrawingcontext *) Context ns_available (10_11 , 7_0);

The meaning of the parameter:

1, size

Limit the maximum width and height, although it is adaptive, but you need to limit the maximum widths and heights.

2. Options

The type is NSStringDrawingOptions used to indicate the rendering options when drawing a string.

Each option is as follows:

 typedef ns_options (Nsinteger, nsstringdrawingoptions) {//The specified origin is the Line fragment origin, not the base line origin//The entire text calculates the size of the entire text in a rectangle consisting of each row Nsstringdrawinguseslinefragmentorigin = 1 &L t;< 0,//Uses the font leading for calculating lines heights//Use the line spacing of the fonts to calculate the range that the text occupies, that is, the distance from the bottom of each line to the bottom of the next line Nsstringdra wingusesfontleading = 1 << 1,//Uses image glyph bounds instead of typographic bounds//To calculate text usage in image notation instead of typesetting The Boundary nsstringdrawingusesdevicemetrics = 1 << 3,//truncates and adds the ellipsis character to the last visible L
 ine if the text doesn ' t fit into the bounds specified.
 Ignored if Nsstringdrawinguseslinefragmentorigin is not also set.
 If the text content exceeds the specified rectangle limit, the text is truncated and the ellipsis is added after the last character. If Nsstringdrawinguseslinefragmentorigin is not set, this option does not take effect nsstringdrawingtruncateslastvisibleline NS_ENUM_AVAILABLE ( 
10_5, 6_0) = 1 << 5, ns_enum_available (10_0, 6_0); 

Third, attributes

The Text property that is applied to the string.

Four, the context

NSStringDrawingContext Type, which controls the amount of kerning and scaling that is used when text is drawn. This parameter is passed into the nil.

Example

NSString *text = @ "is a fine Day";
Uifont *font = [Uifont systemfontofsize:30];
CGRect suggestedrect = [Text Boundingrectwithsize:cgsizemake (A/maxfloat)
            options: Nsstringdrawingusesfontleading
           attributes:@{Nsfontattributename:font}
            Context:nil];
NSLog (@ "size =%@", Nsstringfromcgsize (suggestedrect.size));

Output:size = {200, 35.80078125}

Four different ways to compare

In the case where the font is set to 30, the first two methods used are view returned size = {246.33333333333334, 36} , and the last two methods are used NSString size = {246.3134765625, 35.80078125} . view the use method NSString is slightly larger than the value returned by using the method.

I guess it's all because the text rendering engine needs to set aside a small amount of space at the top and bottom of the label when rendering a line of text, for the sake of typographic beauty.

When displaying fonts of different font size, the string height obtained font size is different from the larger value.

For example, font size for 13 o'clock, calculate the height of the, font size to 20 o'clock, calculate the height of 24.

So the normal setting Uilabel height, also cannot simply in the font height base plus arbitrary value.

Summarize

This is the full content of this article, I hope the content of this article to the iOS developers can help, if you have questions you can message exchange.

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.