'Autoshrink setting for UIButton in Storyboard
There is a setting for UILabel in storyboard that allows setting auto-shrink configurations, as shown below:
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.
Solution 1:[1]
You can use User Defined Runtime Attributes to set this flag using the storyboard.
Set the following key path:
titleLabel.adjustsFontSizeToFitWidth to true
Solution 2:[2]
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;
Solution 3:[3]
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
Solution 4:[4]
Swift 4 solution
class CustomButton : UIButton{
@IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
didSet {
self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
}
}
}
Solution 5:[5]
extension UIButton {
@IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
didSet {
self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
}
}
}
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 | Nicholas |
Solution 2 | Badal Shah |
Solution 3 | techloverr |
Solution 4 | Maor |
Solution 5 | Rahul Dhiman |