ios uilabel 尾部截断

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8985520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:27:43  来源:igfitidea点击:

uilabel tail truncation

objective-ciosuilabel

提问by glogic

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.

我正在使用 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];

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

无论如何,是否可以找到 uilabel 何时实际截断文本,以便我可以检查标签高度并添加到它(如果还有空间)?当有空间时,我总是尝试在高度上添加一条额外的线,这避免了早期截断,但随后我留下了整个标签大小不一致的问题。对此的任何想法都会非常感谢

回答by Stephen Darlington

lineBreakModeis a switch. It can be either(for iOS6+) NSLineBreakByWordWrappingor NSLineBreakByTruncatingTailbut not both.

lineBreakMode是一个开关。它可以是任何(对iOS6的+)NSLineBreakByWordWrappingNSLineBreakByTruncatingTail,但不能同时使用。

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 UILabelappropriately.

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

回答by Hymanslash

Using this method:

使用这种方法:

How to find UILabel's number of Lines

如何找到 UILabel 的行数

You could set the label to the max height, find out how tall the text is in that label and shrink it as necessary.

您可以将标签设置为最大高度,找出该标签中文本的高度并根据需要缩小它。

回答by Alejandro Cotilla

I have written a category for working with UILabel's truncation. Works on iOS 7 and later. Hope it helps !

我编写了一个用于处理 UILabel 截断的类别。适用于 iOS 7 及更高版本。希望能帮助到你 !

@implementation UILabel (Truncation)

- (NSRange)truncatedRange
{
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size];
    textContainer.lineFragmentPadding = 0;
    [layoutManager addTextContainer:textContainer];

    NSRange truncatedrange = [layoutManager truncatedGlyphRangeInLineFragmentForGlyphAtIndex:0];
    return truncatedrange;
}

- (BOOL)isTruncated
{
    return [self truncatedRange].location != NSNotFound;
}

- (NSString *)truncatedText
{
    NSRange truncatedrange = [self truncatedRange];
    if (truncatedrange.location != NSNotFound)
    {
        return [self.text substringWithRange:truncatedrange];
    }

    return nil;
}

@end

回答by Eddie

I just came to a similar problem, and solved it with a very simple solution (tested on ios 8.4, xcode 7).

我刚刚遇到了一个类似的问题,并用一个非常简单的解决方案解决了它(在 ios 8.4、xcode 7 上测试)。

In IB (I use autolayout with some constraints):

在 IB 中(我使用带有一些约束的自动布局):

set UIlabel numberOfLine = 1;
LineBrakeMode = TruncateTail

In Code:

在代码中:

label.numberOfLines = 2; // I can only display 2 row. 
// I supposed you should know how many rows you can displayed.

label.preferredMaxLayoutWidth = label.frame.size.width;
[label sizeToFit];

Tadaa. That's it. Note that this only work with UILabel. With UIButton, it may not work (Haven't tested).

多阿。就是这样。请注意,这仅适用于 UILabel。使用 UIButton,它可能不起作用(尚未测试)。