In interface builder, holding Command + = will resize a button to fit its text. I was wondering if this was possible to do programmatically before the
For some reason, func sizeToFit()
does not work for me. My set up is I am using a button inside a UITableViewCell
and I am using auto layout.
What worked for me is:
intrinsicContentSize
width because according the this document auto layout is not aware of the intrinsicContentSize
. Set the width constraint to the intrinsicContentSize
widthHere're two titles from the Debug View Hierachry
To accomplish this using autolayout, try setting a variable width constraint:
You may also need to adjust your Content Hugging Priority
and Content Compression Resistance Priority
to get the results you need.
UILabel is completely automatically self-sizing:
This UILabel is simply set to be centered on the screen (two constraints only, horizontal/vertical):
It changes widths totally automatically:
You do not need to set any width or height - it's totally automatic.
Notice the small yellow squares are simply attached ("spacing" of zero). They automatically move as the UILabel resizes.
Adding a ">=" constraint sets a minimum width for the UILabel:
To be honest I think that it's really shame that there is no simple checkbox in storyboard to say that you want to resize buttons to accommodate the text. Well... whatever.
Here is the simplest solution using storyboard.
Place UILabel inside UIView. Set constraints to attach it to edges of UIView.
Place your UIButton inside UIView. Set the same constraints to attach it to the edges of UIView.
Set 0 for UILabel's number of lines.
Set up the outlets.
@IBOutlet var button: UIButton!
@IBOutlet var textOnTheButton: UILabel!
let someTitle = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
override func viewDidLoad() {
super.viewDidLoad()
textOnTheButton.text = someTitle
button.setTitle(someTitle, for: .normal)
button.titleLabel?.numberOfLines = 0
}
Swift:
The important thing here is when will you call the .sizeToFit()
, it should be after setting the text to display so it can read the size needed, if you later update text, then call it again.
var button = UIButton()
button.setTitle("Length of this text determines size", forState: UIControlState.Normal)
button.sizeToFit()
self.view.addSubview(button)
I have some additional needs related to this post that sizeToFit
does not solve. I needed to keep the button centered in it's original location, regardless of the text. I also never want the button to be larger than its original size.
So, I layout the button for its maximum size, save that size in the Controller and use the following method to size the button when the text changes:
+ (void) sizeButtonToText:(UIButton *)button availableSize:(CGSize)availableSize padding:(UIEdgeInsets)padding {
CGRect boundsForText = button.frame;
// Measures string
CGSize stringSize = [button.titleLabel.text sizeWithFont:button.titleLabel.font];
stringSize.width = MIN(stringSize.width + padding.left + padding.right, availableSize.width);
// Center's location of button
boundsForText.origin.x += (boundsForText.size.width - stringSize.width) / 2;
boundsForText.size.width = stringSize.width;
[button setFrame:boundsForText];
}
The way to do this in code is:
[button sizeToFit];
If you are subclassing and want to add extra rules you can override:
- (CGSize)sizeThatFits:(CGSize)size;