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


基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01