IPhone: Dynamically get the height and width of Uilabel

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/spiritstudio/archive/2011/11/17/2252074.html

When you use Uilabel to store strings, you often need to get the long-width data for the label, which is a list of some common computational methods.

1. Get the width to get the length of the string without wrapping the row line

Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (Maxfloat, 30)];

Note: The width of size should be set to maxfloat if it is desired.

2. Gets the height that gets the actual height required for the string to wrap within the specified size (the width of the label is more than the line).

Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:uilinebreakmodewordwrap];

Note: The height of size should be set to maxfloat if you want to get high.

3. When actually programming, sometimes you need to calculate the position of the last character of a piece of text, and then add a picture or other control (such as the info icon), the following code is the method to calculate the position of the last character in the label.

Cgsize sz = [Label.text sizeWithFont:label.font constrainedtosize:cgsizemake (Maxfloat, 40)];

Cgsize linessz = [Label.text sizeWithFont:label.font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:uilinebreakmodewordwrap];

if (sz.width <= linessz.width)//Determine if line is broken

{

Lastpoint = Cgpointmake (label.frame.origin.x + sz.width, LABEL.FRAME.ORIGIN.Y);

}

Else

{

Lastpoint = Cgpointmake (label.frame.origin.x + (int) sz.width% (int) linessz.width,linessz.height-sz.height);

}

Article two, transfer from: http://www.cnblogs.com/heri/archive/2013/03/18/2965817.html

In the case of most attributes, when dynamic Data binding is given to Uilabel, it is often necessary to dynamically adjust the width or height of the Uilabel depending on how much the string is.

Here are two things to consider:

1, Uilabel width unchanged, according to the number of fonts, automatically adjust the height of uilabel, and fold line display.

The code is as follows:

  1. UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, 10, 200, 20)];
  2. Label.font = [Uifont boldsystemfontofsize:20.0f]; font size of//uilabel
  3. Label.numberoflines = 0; //must define this property, otherwise Uilabel will not wrap.
  4. Label.textcolor = [Uicolor Whitecolor];
  5. Label.textalignment = Nstextalignmentleft; //Text alignment
  6. [Label Setbackgroundcolor:[uicolor Redcolor];
  7. The width is constant, the height of the label is calculated based on how much the word
  8. NSString *str = @"Can change this content to test, the width is unchanged, the height is automatically adjusted according to the content";
  9. Cgsize size = [str sizeWithFont:label.font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:nslinebreakbywordwrapping];
  10. Reset the size of the Uilabel based on the calculation results
  11. [Label Setframe:cgrectmake (0, ten, $, size.height)];
  12. Label.text = str;
  13. [Self.view Addsubview:label];
  14. [Label release];


2, Uilabel height unchanged, according to the font number, automatically adjust the width of the Uilabel, and fold the line display

The code is as follows:

  1. UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, 10, 20, 20)];
  2. Label.font = [Uifont boldsystemfontofsize:20.0f]; font size of//uilabel
  3. Label.numberoflines = 0; //must define this property, otherwise Uilabel will not wrap.
  4. Label.textcolor = [Uicolor Whitecolor];
  5. Label.textalignment = Nstextalignmentleft; //Text alignment
  6. [Label Setbackgroundcolor:[uicolor Redcolor];
  7. The height of a fixed line, according to the number of words to calculate the width of the label
  8. NSString *str = @"Height invariant gets width, gets the length of the string that is required to display the line lines without folding";
  9. Cgsize size = [str sizeWithFont:label.font constrainedtosize:cgsizemake (maxfloat, label.frame.size.height)];
  10. NSLog (@"size.width=%f, size.height=%f", Size.width, Size.Height);
  11. Reset the size of the Uilabel based on the calculation results
  12. [Label Setframe:cgrectmake (0, ten, Size.width, 20)];
  13. Label.text = str;
  14. [Self.view Addsubview:label];
  15. [Label release];


In either case, the core code is the code at size, and the corresponding size is set to Maxfloat

IPhone: Get the height and width of Uilabel dynamically

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.