UILabel并在自动调整后获取大小

UILabel并在自动调整后获取大小

本文介绍了UILabel并在自动调整后获取大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UILabel实例,其默认字体大小为28,最小字体大小为16.我允许字体自动调整以适应此最小值,并且只能使用一行.

I have an instance of UILabel with a default font size of 28 and minimum font size of 16. I allow the font to auto-adjust to fit subject to this minimum and with one line only.

假设UILabel中有一些文本已自动调整为适合.如何获得新的字体大小,或更确切地说是如何应用应用于标签内容的比例因子?

Suppose I have some text in the UILabel that has been automatically adjusted to fit. How do I get the new font size, or to be more precise the scale factor applied to the label's content?

[NB.字体属性在自动调整下不会更改.]

[NB. The font property does not change under autofit.]

推荐答案

恐怕此计算是在绘制时完成的,并且无法访问(至少使用公共api)

I'm afraid that this calculation is done at draw time and not accessible (at least using public apis)

但是您应该能够使用UIKit sizeWithFont:方法,就像UILabel一样. (请注意,文本指标会消耗CPU资源,就像在表单元格中使用它们一样)

But you should be able to calculate it using one of UIKit sizeWithFont: methods, like UILabel does. (Beware that text metrics are CPU consuming... like when using them in a table cell)

这篇关于UILabel并在自动调整后获取大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:18