How to calculate UILabel width based on text length?

134,026

Solution 1

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                        constrainedToSize:maximumLabelSize 
                        lineBreakMode:yourLabel.lineBreakMode]; 

What is -[NSString sizeWithFont:forWidth:lineBreakMode:] good for?

this question might have your answer, it worked for me.


For 2014, I edited in this new version, based on the ultra-handy comment by Norbert below! This does everything. Cheers

// yourLabel is your UILabel.

float widthIs = 
 [self.yourLabel.text
  boundingRectWithSize:self.yourLabel.frame.size                                           
  options:NSStringDrawingUsesLineFragmentOrigin
  attributes:@{ NSFontAttributeName:self.yourLabel.font }
  context:nil]
   .size.width;

NSLog(@"the width of yourLabel is %f", widthIs);

Solution 2

yourLabel.intrinsicContentSize.width for Objective-C / Swift

Solution 3

In swift

 yourLabel.intrinsicContentSize().width 

Solution 4

The selected answer is correct for iOS 6 and below.

In iOS 7, sizeWithFont:constrainedToSize:lineBreakMode: has been deprecated. It is now recommended you use boundingRectWithSize:options:attributes:context:.

CGRect expectedLabelSize = [yourString boundingRectWithSize:sizeOfRect
                                                    options:<NSStringDrawingOptions>
                                                 attributes:@{
                                                    NSFontAttributeName: yourString.font
                                                    AnyOtherAttributes: valuesForAttributes
                                                 }
                                                    context:(NSStringDrawingContext *)];

Note that the return value is a CGRect not a CGSize. Hopefully that'll be of some assistance to people using it in iOS 7.

Solution 5

Swift 4 Answer who are using Constraint

label.text = "Hello World"

var rect: CGRect = label.frame //get frame of label
rect.size = (label.text?.size(attributes: [NSFontAttributeName: UIFont(name: label.font.fontName , size: label.font.pointSize)!]))! //Calculate as per label font
labelWidth.constant = rect.width // set width to Constraint outlet

Swift 5 Answer who are using Constraint

label.text = "Hello World"

var rect: CGRect = label.frame //get frame of label
rect.size = (label.text?.size(withAttributes: [NSAttributedString.Key.font: UIFont(name: label.font.fontName , size: label.font.pointSize)!]))! //Calculate as per label font
labelWidth.constant = rect.width // set width to Constraint outlet
Share:
134,026
Sheehan Alam
Author by

Sheehan Alam

iOS, Android and Mac Developer. i can divide by zero.

Updated on February 12, 2021

Comments

  • Sheehan Alam
    Sheehan Alam about 3 years

    I want to display an image next to a UILabel, however UILabel has variable text length, so I don't know where to place the image. How can I accomplish this?

  • JED HK
    JED HK over 10 years
    Just a note: this is deprecated since iOS7. The preferred way now is: [yourString boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:yourLabel.font } context:nil];
  • Boris
    Boris almost 10 years
    You can also use the IntrinsicContentSize property. I'm not much into Objective-c, but it should be something like this: self.yourLabel.intrinsicContentSize This will give you the size of the label content, so you can just get the width from there.
  • The Humble Rat
    The Humble Rat almost 9 years
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
  • Honey Lakhani
    Honey Lakhani almost 9 years
    this answer tell how to adjust label size according to text. whats the problem in this?
  • Chandni
    Chandni almost 6 years
    not work for me it is not calculating label's width height on the basis of their text
  • rommex
    rommex almost 6 years
    This answer is valid only for a view that has been laid out
  • heyfrank
    heyfrank over 5 years
    Works perfectly for me, with custom font also!
  • denis_lor
    denis_lor almost 5 years
    Just yourLabel.intrinsicContentSize.width works great, check answer below.
  • nomnom
    nomnom almost 5 years
    Great! Convenient for calculating the width of UIButton according to text, where intrinsicContentSize.width doesn't always work correctly.
  • Chetan
    Chetan over 4 years
    perfect answer for getting width of dynamic label
  • John Pitts
    John Pitts over 2 years
    XCode doesn't like those parentheses.