如何在 Swift 中以编程方式调整 UIButton 中文本的字体大小以适应宽度



我正在尝试以编程方式设置UIButton中的文本,但是文本在按钮中显示时会变短。有没有办法通过代码调整按钮中文本的字体大小以适应宽度?我已经检查了其他问题,但答案并不能解决我的问题。谢谢。

为按钮对象添加以下行:

button.titleLabel?.adjustsFontSizeToFitWidth = true
如果你使用

titleLabel,你可以使用 UILabel 的 adjustsFontSizeToFitWidth

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/index.html#//apple_ref/occ/instp/UILabel/adjustsFontSizeToFitWidth

然后使用 UILabel 的 minimumScaleFactor 设置最小值:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/index.html#//apple_ref/occ/instp/UILabel/minimumScaleFactor

最新更新