Adjust Uibutton Font Size to Width

Adjust UIButton font size to width

Try this:

button.titleLabel?.numberOfLines = 1
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = .byClipping //<-- MAGIC LINE

I'm not sure why this does the trick but it does :)

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

UIButton auto-adjust Button font size Swift

With auto layout you can set the space between buttons, and the max and min size. In code for all labels use:

self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.adjustsFontSizeToFitWidth = YES;

With this all labels adjust the text will size.

For adjust button to titleLabel use auto layout constraint for titleLabel. For examples:

  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.button.titleLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];

This constraint define the height of titleLabel to 50% of self.view height. Now you can adapte the constraint for that you need.

This work in you code?

How to adjust font size of text in a UIButton to fit the width programmatically in Swift?

Add this line for button object:

button.titleLabel?.adjustsFontSizeToFitWidth = true

How to adjust font size of text in a UIButton to fit the height programmatically in Swift?

Have a look at the baselineAdjustment property of UILabel:

It can be used with a UIButton as follows:

button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters

Adjust UIButton label font ,to screen size

You can try using CAT.titleLabel.minimumScaleFactor to set the minimum font size. It takes in a multiplier value against the current font size.



Related Topics



Leave a reply



Submit