当标签中的文本被约束下来适合宽度时,如何计算UIFont的大小? [英] How to calculate the size of a UIFont when text in label is constrained down to fit width?

查看:158
本文介绍了当标签中的文本被约束下来适合宽度时,如何计算UIFont的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下UILabel,它宽200像素,高50像素。标签内部有文字,标签使文字更小,使其适合标签。但现在,你会如何获得UIFont的大小如何在标签中可见?让我们想象字体的大小是巨大的100,标签挤压到15。然后,你想做一些其他标签与小文本,它有相同的字体大小。是否有办法获取UIFont的字体大小后被标签挤压?

Imagine a UILabel, which is 200 pixels wide and 50 pixels high. The label has text inside, and the label makes the text smaller so that it fits into the label. But now, how would you get the size of that UIFont how it is visible in the label? Lets imagine the font size was given with huge 100, and the label squeezes it down to 15. And then, you want to make some other labels with little text, which has same font size. Is there a way to obtain the UIFont's font size after getting squeezed by the label?

推荐答案

如果你通过UILabel的大小和breakMode等:

If you pass the size of the UILabel and the breakMode, etc. to:

CGSize  size = [label.text sizeWithFont:label.font minFontSize:10 actualFontSize:&actualFontSize forWidth:200 lineBreakMode:UILineBreakModeTailTruncation];

actualFontSize应该是您要查找的内容。

actualFontSize should be what you are looking for.

更新:

上述内容已被弃用。现在使用的方法是:

The above has been deprecated. The method to use now is:

- (CGRect)textRectForBounds:(CGRect)bounds
     limitedToNumberOfLines:(NSInteger)numberOfLines

以下是一个示例

CGSize  size = [label textRectForBounds:label.bounds
     limitedToNumberOfLines:1].size;

这篇关于当标签中的文本被约束下来适合宽度时,如何计算UIFont的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆