当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算 UILabel 的字体大小?

How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?(当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算 UILabel 的字体大小?)

本文介绍了当 -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 的字体大小?