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 button was added to the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// I need to know the width needed to accomodate NSStringVariable
[button setTitle:NSStringVariable forState:UIControlStateNormal];
// So that I can set the width property of the button before addSubview
[button setFrame:CGRectMake(10, 0, width, fixedHeight)];
[subNavigation addSubview:button];
In UIKit, there are additions to the NSString class to get from a given NSString object the size it'll take up when rendered in a certain font.
Docs was here. Now it's here under Deprecated.
In short, if you go:
CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:14]];
//or whatever font you're using
[button setFrame:CGRectMake(10,0,stringsize.width, stringsize.height)];
...you'll have set the button's frame to the height and width of the string you're rendering.
You'll probably want to experiment with some buffer space around that CGSize, but you'll be starting in the right place.
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;
If your button was made with Interface Builder, and you're changing the title in code, you can do this:
[self.button setTitle:@"Button Title" forState:UIControlStateNormal];
[self.button sizeToFit];
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:
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)
If you want to resize the text as opposed to the button, you can use ...
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.minimumScaleFactor = .5;
// The .5 value means that I will let it go down to half the original font size
// before the texts gets truncated
// note, if using anything pre ios6, you would use I think
button.titleLabel.minimumFontSize = 8;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With