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

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

问题描述

想象一个 200 像素宽和 50 像素高的 UILabel.标签里面有文本,标签使文本更小,以便它适合标签.但是现在,您将如何获得 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天全站免登陆