How to calculate UILabel width based on text length?
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
Sheehan Alam
iOS, Android and Mac Developer. i can divide by zero.
Updated on February 12, 2021Comments
-
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 over 10 yearsJust a note: this is deprecated since iOS7. The preferred way now is:
[yourString boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:yourLabel.font } context:nil];
-
Boris almost 10 yearsYou 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 almost 9 yearsThis 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 almost 9 yearsthis answer tell how to adjust label size according to text. whats the problem in this?
-
Chandni almost 6 yearsnot work for me it is not calculating label's width height on the basis of their text
-
rommex almost 6 yearsThis answer is valid only for a view that has been laid out
-
heyfrank over 5 yearsWorks perfectly for me, with custom font also!
-
denis_lor almost 5 yearsJust
yourLabel.intrinsicContentSize.width
works great, check answer below. -
nomnom almost 5 yearsGreat! Convenient for calculating the width of UIButton according to text, where intrinsicContentSize.width doesn't always work correctly.
-
Chetan over 4 yearsperfect answer for getting width of dynamic label
-
John Pitts over 2 yearsXCode doesn't like those parentheses.