uilabel尾截断 [英] uilabel tail truncation

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

问题描述

我正在使用目标c处理ios应用程序,我对uilabel有一个问题,我可以使用一些帮助。基本上我有一个标签,可以改变大小以适应它将显示的文本,但它有一个可能的最大高度。标签本身始终具有固定的宽度。我已经打开了UILineBreakModeWordWrap和UILineBreakModeTailTruncation来使文本适合并截断,但这会导致文本在只剩下1个单词时过早地截断尾部。而不是当它仍有空间时将其移动到下一行它只是截断它。

Im working on an ios app using objective c and i have an issue with uilabel that i could use some help with. Basically i have a label that can change size to fit the text that it will display but it has a max height that it can possible be. the label itself has a fixed width at all times. i have turned on UILineBreakModeWordWrap and UILineBreakModeTailTruncation to make the text fit and truncate but this causes the text to truncate the tail too early when it has only 1 word left to place. rather then moving it onto the next line when there is still room it just truncates it.

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
self.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
self.numberOfLines = 0;
[self sizeToFit];

无论如何找到uilabel实际截断文本的时候我可以检查标签高度如果还有空间,还要加上它?当有空间时,我总是尝试在高度上添加一条额外的线,这样可以避免早期截断,但是我会在整个标签上留下不一致的尺寸。任何想法都会非常感谢

is there anyway of finding when the uilabel is actually truncating the text so i can then check the label height and add to it if there is still room ? I tried always adding an extra line to the height when there is room and this avoids the early truncation but then im left with inconsistent sizing of the over all label. any ideas on this would be great thanks

推荐答案

lineBreakMode 是一个开关。它可以是 (适用于iOS6 +) NSLineBreakByWordWrapping NSLineBreakByTruncatingTail 但不能同时使用。

lineBreakMode is a switch. It can be either (for iOS6+) NSLineBreakByWordWrapping or NSLineBreakByTruncatingTail but not both.

但是,要回答您的问题,您可以使用 的NSString + UIKit的 。找到大小后,您可以适当地更新 UILabel 的框架。

But, to answer your question, you can find the size of some text using the class extensions in NSString+UIKit. Having found the size you could update the frame of the UILabel appropriately.

这篇关于uilabel尾截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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