uilabel 尾部截断
我正在使用 Objective C 开发一个 ios 应用程序,并且我遇到了 uilabel 的问题,我需要一些帮助。基本上我有一个标签,可以更改大小以适应它将显示的文本,但它有一个可能的最大高度。标签本身始终具有固定宽度。我已打开 UILineBreakModeWordWrap 和 UILineBreakModeTailTruncation 以使文本适合并截断,但这会导致文本在只剩下 1 个单词要放置时过早截断尾部。而不是在仍有空间时将其移动到下一行,而是将其截断。
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
self.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
self.numberOfLines = 0;
[self sizeToFit];
无论如何,是否可以找到 uilabel 何时实际截断文本,以便我可以检查标签高度并在仍有空间时添加到它?当有空间时,我总是尝试在高度上添加一条额外的线,这可以避免早期截断,但随后我会发现整个标签的尺寸不一致。任何关于这方面的想法都会非常感谢
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];
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
lineBreakMode
是一个开关。它可以是(对于 iOS6+)NSLineBreakByWordWrapping
或NSLineBreakByTruncatingTail
,但不能同时是两者。但是,要回答您的问题,您可以使用
NSString+UIKit
。找到尺寸后,您可以适当地更新UILabel
的框架。lineBreakMode
is a switch. It can be either (for iOS6+)NSLineBreakByWordWrapping
orNSLineBreakByTruncatingTail
but not both.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 theUILabel
appropriately.我刚刚遇到类似的问题,并用一个非常简单的解决方案解决了它(在ios 8.4,xcode 7上测试)。
在 IB 中(我使用带有一些限制的自动布局):
在代码中:
Tadaa。就是这样。请注意,这仅适用于 UILabel。对于 UIButton,它可能不起作用(尚未测试)。
I just came to a similar problem, and solved it with a very simple solution (tested on ios 8.4, xcode 7).
In IB (I use autolayout with some constraints):
In Code:
Tadaa. That's it. Note that this only work with UILabel. With UIButton, it may not work (Haven't tested).
使用此方法:
如何查找 UILabel 的行数
您可以设置将标签调整到最大高度,找出该标签中文本的高度并根据需要缩小它。
Using this method:
How to find UILabel's number of Lines
You could set the label to the max height, find out how tall the text is in that label and shrink it as necessary.
我编写了一个用于处理 UILabel 截断的类别。适用于 iOS 7 及更高版本。希望有帮助!
I have written a category for working with UILabel's truncation. Works on iOS 7 and later. Hope it helps !