当标签中的文本被限制为适合宽度时,如何计算 UIFont 的大小?

How to calculate the size of a UIFont when text in label is constrained down to fit width?(当标签中的文本被限制为适合宽度时,如何计算 UIFont 的大小?)
本文介绍了当标签中的文本被限制为适合宽度时,如何计算 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 的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)