Adjust font size of text to fit in UIButton(调整文本的字体大小以适应 UIButton)
问题描述
我想确保按钮文本适合 UIButton,而 UIButton 具有固定大小.
I want to make sure the button text fits into a UIButton, while the UIButton has a fixed size.
当然我可以访问 UIButton 的 titleLabel.在标签中,我会将 autoshrink
设置为似乎对应于
Of course I can access the titleLabel of the UIButton.
In a label I would set autoshrink
to minimum font scale which seems to correspond to
self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
,但实际上并不相同,因为它只会使文本水平适应边界,而不是垂直适应,因此不会改变字体大小.
, but doesn't really behave the same, since it only makes the text fits horizontally into the bounds, not vertically, thereby not changing the font size.
如何以编程方式实际调整标签的字体大小以使文本适合标签边界(如下图的目标所示)?
How can i actually adjust the font size of a label programmatically to make the text fit into the label bounds (as shown in Goal in the picture below) ?
我已经试过了
self.myButton.titleLabel.numberOfLines = 0;
self.myButton.titleLabel.minimumScaleFactor = 0.5f;
没有成功,总是以上图左侧的 adjustsFontSizeToFitWidth
结束.
without success, always ended up as in adjustsFontSizeToFitWidth
on the left side of the pic above.
编辑:解决方案还必须兼容 ios7
推荐答案
self.mybutton.titleLabel.minimumScaleFactor = 0.5f;
self.mybutton.titleLabel.numberOfLines = 0; <-- Or to desired number of lines
self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES;
... 在 viewDidLoad 中 layoutIfNeeded 之后成功了事实证明,所有这些都必须设置为实际调整字体大小,而不仅仅是使其适合框架.
... did the trick, after layoutIfNeeded in viewDidLoad As it turns out, all those must be set to actually adjust the font-size, not just making it fit into the frame.
mybutton.titleLabel?.minimumScaleFactor = 0.5
mybutton.titleLabel?.numberOfLines = 0 <-- Or to desired number of lines
mybutton.titleLabel?.adjustsFontSizeToFitWidth = true
这篇关于调整文本的字体大小以适应 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调整文本的字体大小以适应 UIButton


基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01