How to adjust font size of text in a UIButton to fit the width programmatically in Swift?

拟墨画扇 提交于 2019-12-08 15:29:14

问题


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.


回答1:


Add this line for button object:

button.titleLabel?.adjustsFontSizeToFitWidth = true



回答2:


If you work with the titleLabel you can use UILabel's adjustsFontSizeToFitWidth

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

and then use UILabel's minimumScaleFactor to set a minimum:

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



来源:https://stackoverflow.com/questions/27561156/how-to-adjust-font-size-of-text-in-a-uibutton-to-fit-the-width-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!