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 的字体大小?


- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01