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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:37:18  来源:igfitidea点击:

Autoshrink setting for UIButton in Storyboard

iosxcodexcode-storyboard

提问by Evol Gate

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

在storyboard中有一个UILabel的设置,可以设置自动收缩的配置,如下图:

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.

但是我无法为 UIButton 的文本标签找到相同的内容。我知道我可以通过编程方式进行设置,但很想知道是否有办法为 Storyboard 中的 UIButton 启用此设置。

回答by Nicholas

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

使用 Storyboard 调整字体大小

回答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

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

参考http://nshipster.com/ibinspectable-ibdesignable/

回答by Maor

Swift 4 solution

斯威夫特 4 解决方案

class CustomButton : UIButton{
    @IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
        didSet {
            self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
        }
    }
}