iPhone 如何识别 UILabels 是否会显示截断文本? [英] iPhone how to identify whether UILabels will show truncated text?

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

问题描述

UILineBreakModeTailTruncation 启动时,我需要在我的 UILabel 上显示更多按钮.即每当..."出现时,我需要通过一些操作显示我的更多按钮.

I need to show a more button on my UILabel when UILineBreakModeTailTruncation starts. ie whenever the "..." appears, I need to display my more button with some actions.

我正在做的是

float textWidth = [myString sizeWithFont:myLabel.font].width;
if (textWidth > myLabel.frame.size.width)
{
[moreButton setHidden:FALSE];
}
else
{
[moreButton setHidden:TRUE];
}

但我的问题是,当标签的行数设置为 2 时,每当渲染标签的第一行时,就会显示更多按钮.

But my problem is, when the number of lines of the label is set to 2, more button is shown whenever the first line of the label is rendered.

所以我试过了

if (textWidth > 2*myLabel.frame.size.width)
{
[moreButton setHidden:FALSE];
}
else
{
[moreButton setHidden:TRUE];
}

这在大多数情况下都有效.但在某些情况下,文本宽度与 2* labelsWidth 相同,会显示更多按钮.有什么直接的方法吗?

This works in most cases. But in some cases where text width is same as that of 2* labelsWidth, more button is shown. Is there any direct method to this?

推荐答案

可以使用这个函数来获取字符串的大小:

You can use this function to get the size of the string:

CGSize size=[myLabel.text sizeWithFont:[UIFont systemFontOfSize:h] constrainedToSize:CGSizeMake(maxWidth, maxHeight) lineBreakMode:UILineBreakModeTailTruncation];

您还应该检查其他 sizeWithFont: 函数.

You should also check the other sizeWithFont: functions.

这篇关于iPhone 如何识别 UILabels 是否会显示截断文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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