swift: programmatically create UILabel fixed width that resizes vertically according to text length
There are two usefull methods of UIView: sizeToFit() and sizeThatFits(_:)
The first one resizes a view to a minimal size to fit subviews' content and the second one doesn't change frame at all, but returns calculated size which: (1) fit all subviews and (2) doesn't exceed parameter size
So you can use sizeThatFits
for you purpose:
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
label.backgroundColor = UIColor.orange
label.textColor = UIColor.white
// label.text = "ultimate Frisbee"
label.text = "ultimate Frisbee\nin 3 minutes,\nall welcome|2"
label.numberOfLines = 10
view.addSubview(label)
updateLabelFrame()
}
func updateLabelFrame() {
let maxSize = CGSize(width: 150, height: 300)
let size = label.sizeThatFits(maxSize)
label.frame = CGRect(origin: CGPoint(x: 100, y: 100), size: size)
}
Output:
P.S. You also can solve your problem with autolayout constraints, but I am not a big fan of using them programmatically.
Adjust UILabel height depending on the text
sizeWithFont constrainedToSize:lineBreakMode:
is the method to use. An example of how to use it is below:
//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
UILabel - auto-size label to fit text?
Please check out my gist where I have made a category for UILabel
for something very similar, my category lets a UILabel
stretch it's height to show all the content: https://gist.github.com/1005520
Or check out this post: https://stackoverflow.com/a/7242981/662605
This would stretch the height, but you can change it around easily to work the other way and stretch the width with something like this, which is I believe what you want to do:
@implementation UILabel (dynamicSizeMeWidth)
- (void)resizeToStretch{
float width = [self expectedWidth];
CGRect newFrame = [self frame];
newFrame.size.width = width;
[self setFrame:newFrame];
}
- (float)expectedWidth{
[self setNumberOfLines:1];
CGSize maximumLabelSize = CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX);
CGSize expectedLabelSize = [[self text] sizeWithFont:[self font]
constrainedToSize:maximumLabelSize
lineBreakMode:[self lineBreakMode]];
return expectedLabelSize.width;
}
@end
You could more simply use the sizeToFit
method available from the UIView
class, but set the number of lines to 1 to be safe.
iOS 6 update
If you are using AutoLayout, then you have a built in solution. By setting the number of lines to 0, the framework will resize your label appropriately (adding more height) to fit your text.
iOS 8 update
sizeWithFont:
is deprecated so use sizeWithAttributes:
instead:
- (float)expectedWidth{
[self setNumberOfLines:1];
CGSize expectedLabelSize = [[self text] sizeWithAttributes:@{NSFontAttributeName:self.font}];
return expectedLabelSize.width;
}
How to calculate UILabel width based on text length?
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.
// 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);
resizing UILabels based on total width of text
If Want to Make size of UILabel Based on TextSize and TextLength then use
intrinsicContentSize.
Below is sample code for that:
lblDemo.frame.size = lblDemo.intrinsicContentSize
Here lblDemo is IBOutlet of UILabel.
Related Topics
Gmsmarker Icon in the Top Left Corner of the View (Ios)
Save Depth Images from Truedepth Camera
Swift - Parse a String Which Contains a Url
iOS Keyboard Active But Invisible When Uisearchbar Is Tapped
How to Test If "Allow Full Access" Permission Is Granted from Containing App
Show Two Different Custom Cells in Same Uitableview - Swift Firebase
(Swift Spritekit) Rotate Sprite in the Direction of Touch
Today Extension with Uicollectionview Different Behaviour Compared to Single View Application
Passing Data Between View Controllers Without Segue
Setting Uiimageview Image Affects Layout Constraints
How to Get All Keys and Values into Separate String Arrays from Nsdictionary in Swift
How to Access Index of Tabbar Item Using Swift
Show Bounding Box While Detecting Object Using Arkit 2
Correct Way to Layout Swiftui (Similar to Autolayout)
Swiftui Multiple Navigationlinks in Form/Sheet - Entry Stays Highlighted