如何在 Swift 中以编程方式调整 UIButton 中文本的字体大小以适应宽度? [英] How to adjust font size of text in a UIButton to fit the width programmatically in Swift?

查看:88
本文介绍了如何在 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

这篇关于如何在 Swift 中以编程方式调整 UIButton 中文本的字体大小以适应宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆