How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?(当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算 UILabel 的字体大小?)
问题描述
当myLabel.adjustsFontSizeToFitWidth = YES 时,UILabel 会自动调整字体大小,以防标签的文本过长.例如,如果我的标签只有 100 像素宽,而我的文本太长而无法适应当前的字体大小,它会缩小字体大小,直到文本适合标签为止.
When myLabel.adjustsFontSizeToFitWidth = YES, UILabel will adjust the font size automatically in case the text is too long for the label. For example, if my label is just 100px wide, and my text is too long to fit with the current font size, it will shrink down the font size until the text fits into the label.
当字体大小缩小时,我需要从 UILabel 获取实际显示的字体大小.例如,假设我的字体大小实际上是 20,但 UILabel 不得不将其缩小到 10.当我向 UILabel 询问字体和字体大小时,我得到了我的旧字体大小(20),但不是显示的那个(10).
I need to get the actual displayed font size from UILabel when the font size got shrunk down. For example, let's say my font size was actually 20, but UILabel had to shrink it down to 10. When I ask UILabel for the font and the font size, I get my old font size (20), but not the one that's displayed (10).
推荐答案
我不确定这是否完全准确,但希望它应该非常接近.它可能不考虑截断的字符串或标签的高度,但您可以手动完成.
I'm not sure if this is entirely accurate, but it should be pretty close, hopefully. It may not take truncated strings into account, or the height of the label, but that's something you might be able to do manually.
方法
- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode一个>
将返回文本大小,并注意它还有一个用于实际使用的字体大小的参考参数.
will return the text size, and notice that it also has a reference parameter for the actual font size used.
这篇关于当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算 UILabel 的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算 UILabel 的字体大小?
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
