如何在 Swift 中以编程方式调整 UIButton 中文本的字体大小以适应宽度?
问题描述:
我试图以编程方式在我的 UIButton
中设置文本,但在按钮中显示时文本变得缩写.有没有办法通过代码调整按钮中文本的字体大小以适应宽度?我已经检查了其他问题,但答案并不能解决我的问题.谢谢.
I am trying to set the text in my UIButton
programmatically, but the text becomes abbreviated when displayed in the button. Is there a way to adjust the font size of the text in the button to fit the width, through code? I have checked other questions, but the answers do not solve my problem. Thanks.
答
为按钮对象添加这一行:
Add this line for button object:
button.titleLabel?.adjustsFontSizeToFitWidth = true