xcode 故事板中 UIButton 的自动收缩设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35860102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autoshrink setting for UIButton in Storyboard
提问by Evol Gate
There is a setting for UILabel in storyboard that allows setting auto-shrink configurations, as shown below:
在storyboard中有一个UILabel的设置,可以设置自动收缩的配置,如下图:
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.
但是我无法为 UIButton 的文本标签找到相同的内容。我知道我可以通过编程方式进行设置,但很想知道是否有办法为 Storyboard 中的 UIButton 启用此设置。
回答by Nicholas
回答by Badal Shah
No, there is no option available in storyboard for set Button's textlabel auto-shrink ,
不,情节提要中没有可用于设置按钮文本标签自动收缩的选项,
But you can set it programatically with adjustsFontSizeToFitWidthas you are aware with it.
但是您可以使用adjustsFontSizeToFitWidth以编程方式设置它,因为您知道它。
yourbutton.titleLabel?.adjustsFontSizeToFitWidth = true;
回答by techloverr
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
如果你想在故事板中设置尝试 IBDesignable 和 IBInspectable
回答by Maor
Swift 4 solution
斯威夫特 4 解决方案
class CustomButton : UIButton{
@IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
didSet {
self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
}
}
}