theDC
theDC

Reputation: 6484

How to apply auto shrinking to UIButton title?

Imagine the following example:

There is a button like this

enter image description here

The text is set programmatically since it is timer dependent. I want the title font size adjust to the bounds of the button. For typical UILabel, after constraints are set, it is easy to apply auto-shrinking. Is there any way to do it to the title as it is possible for normal UILabel?

I tried

self.alertButton.titleLabel?.sizeToFit()

but it does nothing. Also in interface builder I cannot see such option as it is in case of typical UILabel.

Thanks

Upvotes: 1

Views: 772

Answers (1)

rmaddy
rmaddy

Reputation: 318934

Your question isn't overly clear but I'm guessing you want the button's label to automatically use a smaller font if the length of the label is too long for the button. Is this correct?

Assuming so, simply do:

self.alertButton.titleLabel?.adjustsFontSizeToFitWidth = true;

Upvotes: 7

Related Questions