Easiest way to Adjust UIButton Size based on Text
Using Autolayout, add a width constraint, then set the width constraint's relation to be 'less than or equal' to the constant (max width). The UIButton should auto resize to fit just the text till the max as long as you do not have another fixed width constraint or spacing constraint.
UPDATE:
For this to work with different screen sizes, instead of adding a width constraint, add a trailing space (right side) constraint to the superview (for example). Then set that to 'more than or equal' like maybe 20pts. This means the spacing will always adjust itself to be as large as possible without going off screen (width autoresized to fit text).
How do I resize a UIButton to fit the text without it going wider than the screen?
I managed to get it working in a category of UIButton:
@interface UIButton (ExpandsVertically)
- (CGSize)sizeThatFits:(CGSize)size;
@end
@implementation UIButton (Expandable)
- (CGSize)sizeThatFits:(CGSize)size {
// for the width, I subtract 24 for the border
// for the height, I use a large value that will be reduced when the size is returned from sizeWithFont
CGSize tempSize = CGSizeMake(size.width - 24, 1000);
CGSize stringSize = [self.titleLabel.text
sizeWithFont:self.titleLabel.font
constrainedToSize:tempSize
lineBreakMode:UILineBreakModeWordWrap];
return CGSizeMake(size.width - 24, stringSize.height);
}
@end
If anyone uses this make sure to set:
myButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
Autolayout resize button based on text and have textfield fill available space
Try these autolayout constraints:
Basically:
UITextField
object's width constraint should be aLess Than or Equal
relationUIButton
object's width constraint should be aGreater Than or Equal
relationUITextField
object's horizontalContent Hugging Priority
should be 249 (as per my constraint settings shown above)
And related code as:
[buttonObject setTitle:@"Button title is too... zzz" forState:UIControlStateNormal];
[buttonObject sizeToFit];
...or something along these lines
UIButton that resizes to fit its titleLabel
I've gotten this to work, but you have to use a custom button, not a system type. Give the button both width and height constraints, and make an IBOutlet to the height constraint (heightCon in my code) so you can adjust it in code.
- (void)viewDidLoad {
[super viewDidLoad];
self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.button setTitle:@"A real real real real real real real real long long name." forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
self.button.backgroundColor = [UIColor redColor];
self.button.titleLabel.backgroundColor = [UIColor blueColor];
[self.button layoutIfNeeded]; // need this to update the button's titleLabel's size
self.heightCon.constant = self.button.titleLabel.frame.size.height;
}
After Edit:
I found that you can also do this more simply, and with a system button if you make a subclass, and use this code,
@implementation RDButton
-(CGSize)intrinsicContentSize {
return CGSizeMake(self.frame.size.width, self.titleLabel.frame.size.height);
}
The overridden intrinsicContentSize method is called when you set the title. You shouldn't set a height constraint in this case.
Adjust font size of text to fit in UIButton
self.mybutton.titleLabel.minimumScaleFactor = 0.5f;
self.mybutton.titleLabel.numberOfLines = 0; <-- Or to desired number of lines
self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES;
... did the trick, after layoutIfNeeded in viewDidLoad
As it turns out, all those must be set to actually adjust the font-size, not just making it fit into the frame.
Update for Swift 3:
mybutton.titleLabel?.minimumScaleFactor = 0.5
mybutton.titleLabel?.numberOfLines = 0 <-- Or to desired number of lines
mybutton.titleLabel?.adjustsFontSizeToFitWidth = true
Related Topics
Enable Application Cache in Wkwebview
Restricting App Installations from Appstore Only to Users with iPhone 5/5S/5C
App Installation Failed Due to Application-Identifier Entitlement
Draw a Circle of 1000M Radius Around Users Location in Mkmapview
Autolayout: Add Constraint to Superview and Not Top Layout Guide
How to Play Mp3 Audio from Url in iOS Swift
How to Set Kerning in iPhone Uilabel
Popover in Swift 3 on iPhone iOS
Why Doesn't My Cordova/Phonegap iOS App Rotate When the Device Rotates
Xcode: "This App Could Not Be Installed at This Time."
-Canopenurl: Failed for Url: "Fbauth2:/" (Osstatus Error -10814.)"
Reasons for Rejecting iPhone Application by Apple Store
How to Create a Simple Checkbox in iOS
How to Apply Multiple Transforms in Swift
How to Add Strings on X Axis in iOS-Charts
iOS Enterprise Ota Distribution Unable to Download Application