'UIButton - How to set Minimum Font Size?
My question is simple. Is there any way to set a minimum font size for UIButton like this can be easily set up with UILabel?
Solution 1:[1]
Anoop's answer works like a charm. You can also do this in IB using User Defined Runtime Attributes:
- choose Button in IB
- Add Attribute
- KeyPath: titleLabel.adjustsFontSizeToFitWidth
- Type: Boolean
- Value: true
Screenshot from IB
Solution 2:[2]
Set Minimumfontsize is not available in iOS8 use minimumscalefactor instead.
Solution 3:[3]
in Swift 4/5
Please make sure that yourBtn is available in @IBoutLet, not in action
in viewdidLoad or viewWillAppear
override func viewDidLoad() {
super.viewDidLoad()
yourBtn.titleLabel?.numberOfLines = 1
yourBtn.titleLabel?.adjustsFontSizeToFitWidth = true
yourBtn.titleLabel?.lineBreakMode = NSLineBreakMode.byClipping
}
Solution 4:[4]
For Swift 5.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
YourButton.titleLabel?.numberOfLines = 1
YourButton.titleLabel?.adjustsFontSizeToFitWidth = true
}
Solution 5:[5]
Teetz's answer works like a charm. In case if you want to add margin between titleLabel of UIButton edges after it adjustsFontSizeToFitWidth, you can add few spaces before first character & after last character. So, that it looks neat.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Teetz |
| Solution 2 | iOSdev |
| Solution 3 | Nicholas |
| Solution 4 | Parth Bhavsar |
| Solution 5 | Anish |
