问题描述
我有一个 UILabel
,根据我的应用程序是在iPhone还是iPad上以纵向或横向模式运行,可以有不同的长度。当文本太长而无法在一行显示并且它被截断时,我希望用户能够按下它并获得全文的弹出窗口。
I have a UILabel
that can be varying lengths depending on whether or not my app is running in portrait or landscape mode on an iPhone or iPad. When the text is too long to show on one line and it truncates I want the user to be able to press it and get a popup of the full text.
如何检查 UILabel
是否截断文本?它甚至可能吗?现在我只是根据我所处的模式检查不同的长度,但它不能很好地工作。
How can I check to see if the UILabel
is truncating the text? Is it even possible? Right now I'm just checking for different lengths based on what mode I'm in but it does not work super well.
推荐答案
您可以计算并查看宽度大于 label.bounds.size.width
You can calculate the width of the string and see if the width is greater than label.bounds.size.width
有几种方法可以用特定字体计算字符串的大小。但是,如果您的标签有minimumFontSize,则允许系统将文本缩小到该大小。您可能想要使用在这种情况下。
NSString UIKit Additions has several methods for computing the size of the string with a specific font. However, if you have a minimumFontSize for your label that allows the system to shrink the text down to that size. You may want to use sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: in that case.
CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
if (size.width > label.bounds.size.width) {
...
}
这篇关于如何检查UILabel是否被截断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!