如果UILabel的内容不适合,请更改文本末尾的默认“...” [英] Change the default '...' at the end of a text if the content of a UILabel doesn't fit

查看:88
本文介绍了如果UILabel的内容不适合,请更改文本末尾的默认“...”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone项目中有一个UILabel,它具有固定的宽度和高度,但它的内容可能因用户所看到的而异。有时文本对于UILabel来说很大,那就是当字符串:'...'被添加到行尾时。我想知道我是否可以将此字符串更改为其他内容,例如:'(更多)'。

I have a UILabel in my iPhone project that has a fixed width and height but it's content can vary depending what the user is looking at. Sometimes the text is to big for the UILabel and that is when the string: '...', gets added to the end of the line. I wonder if I could change this string to something else, for instance: '(more)'.

谢谢!

推荐答案

不幸的是,根据这个类似的问题,iOS上似乎没有包含这样的选项:如何更改UILabel中的截断字符?

Unfortunately, it appears that such an option is not included on iOS, as per this similar question: How to change truncate characters in UILabel?

然而,正如前面提到的答案问题状态,这可以很容易地自己完成。您真正需要做的就是找到字符串被截断的位置并减去所选结束字符所需的数量。然后将余数放在一个单独的字符串中。

However, as the answers in the aforementioned question state, this can be easily done yourself. All you really need to do is find where the string gets truncated and subtract the amount needed for your chosen ending character(s). Then put the remainder in a separate string.

对于这个方法,这个答案也很有用:

For this method, this answer would also be useful:


正如Javanator所说,你必须自己截断。你shuld使用sizeWithFont:forWidth:lineBreakMode:消息对UIKit添加到NSString类以获取具有特定字体的字符串的宽度。这将处理所有类型的字体。

As Javanator said you would have to do your own truncation. You shuld use the sizeWithFont:forWidth:lineBreakMode: message on the UIKit additions to NSString class to get the width of a string with a certain font. This will handle all types of fonts.

这篇关于如果UILabel的内容不适合,请更改文本末尾的默认“...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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