43

There is a setting for UILabel in storyboard that allows setting auto-shrink configurations, as shown below:

enter image description here

But I am unable to find the same for UIButton's textlabel. I am aware that I can set this programmatically but curious to know if there's a way to enable this setting for UIButton in Storyboard.

Community
  • 1
  • 1
Evol Gate
  • 2,247
  • 3
  • 19
  • 37

5 Answers5

72

You can use User Defined Runtime Attributes to set this flag using the storyboard.

Set the following key path:

titleLabel.adjustsFontSizeToFitWidth to true

Adjust Font Size using Storyboard

Nicholas
  • 5,770
  • 1
  • 20
  • 30
20

No, there is no option available in storyboard for set Button's textlabel auto-shrink ,

But you can set it programatically with adjustsFontSizeToFitWidth as you are aware with it.

yourbutton.titleLabel?.adjustsFontSizeToFitWidth = true;
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
9

try this

btn.titleLabel.adjustsFontSizeToFitWidth = YES;
btn.titleLabel.minimumScaleFactor = 0.5; // set whatever factor you want to set 

If you want to set in storyboard try IBDesignable and IBInspectable

refer http://nshipster.com/ibinspectable-ibdesignable/

techloverr
  • 2,597
  • 1
  • 16
  • 28
4

Swift 4 solution

class CustomButton : UIButton{
    @IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
        didSet {
            self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
        }
    }
}
Maor
  • 3,340
  • 3
  • 29
  • 38
0
extension UIButton {
    @IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
        didSet {
            self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
        }
    }
}
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. –  May 05 '22 at 00:57