Ios: Uibutton Resize According to Text Length

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:

23226220_Autolayout_Constraints


Basically:

  1. UITextField object's width constraint should be a Less Than or Equal relation
  2. UIButton object's width constraint should be a Greater Than or Equal relation
  3. UITextField object's horizontal Content 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



Leave a reply



Submit