如何检查UILabel是否被截断? [英] How to check if UILabel is truncated?

查看:397
本文介绍了如何检查UILabel是否被截断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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

NSString UIKit Additions 有几种方法可以用特定字体计算字符串的大小。但是,如果您的标签有minimumFontSize,则允许系统将文本缩小到该大小。您可能想要使用 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:在这种情况下。

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是否被截断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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