iOS多行标签NSLineBreakByTruncatingTail

时间:2014-03-03 01:04:40

标签: ios uilabel

我的标签numberOfLines设置为2,lineBreakMode设置为NSLineBreakByTruncatingTail。我期待当文本超出两行时,标签将用椭圆截断它。但正在发生的事情是,尽管文本被截断,但没有显示任何省略号。

为什么会这样?感谢。

更多细节: 以下是包含所讨论标签(contentLabel)的UITableViewCell的自定义类。标签以loadConversation方法更新。

@interface CPActivityStreamCell ()
@property(nonatomic, weak) CardDeck *cardDeck;
@property(nonatomic, weak) UIImageView *icon;
@end

@implementation CPActivityStreamCell

@synthesize contentLabel, eventLabel, avatarView;

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    // Initialize views
    [contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [contentLabel setNumberOfLines:2];
    [contentLabel setTextAlignment:NSTextAlignmentLeft];
    [contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
    contentLabel.backgroundColor = [UIColor clearColor];

    [eventLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [eventLabel setNumberOfLines:1];
    [eventLabel setTextAlignment:NSTextAlignmentLeft];
    [eventLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
    eventLabel.backgroundColor = [UIColor clearColor];
    eventLabel.font = [UIFont fontWithName:@"Antonio-Regular" size:12];

    // Create the icon view
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 15.0, 16.0, 16.0)];
    [self addSubview:icon];
    self.icon = icon;

    CardDeck *deck = [[CardDeck alloc] initWithFrame:self.contentView.frame numberOfCards:1];
    self.backgroundView = deck;
    self.cardDeck = deck;
}

- (void)loadConversation:(CPConversation *)conversation {
    [self loadAvatars:conversation.avatarUrls];
    self.contentLabel.textColor = [UIColor blackColor];
    self.eventLabel.text = [conversation.eventTitle uppercaseString];
    self.eventLabel.textColor = [UIColor colorWithWhite:0.55 alpha:1.0];
    self.contentLabel.text = conversation.content;
    [self loadEventTypeIcon:conversation.eventType white:NO];
    [self updateDeck:conversation withFrame:self.contentView.frame];
    if ([conversation.typeString isEqualToString:@"image"]) {
        [self loadConversationImage:conversation];
    }
}

//other methods

@end

IB中的相同标签:

enter image description here

输出: 顶部单元格的contentLabel设置为Start of a really long text to test truncation and display in activity stream. It should be truncated with ellipses. End.

enter image description here

1 个答案:

答案 0 :(得分:0)

问题出在初始化过程的架构中,或者可能出现在其他地方(在您的问题中没有显示的代码中)。你在说

[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];

但是这段代码没有应用于我们在屏幕截图中看到的标签。这很清楚,因为如果 被应用,文本将是浅灰色。

因此我们可以得出结论,这段代码没有运行,或者如果它正在运行,contentLabel没有连接属性(可能是零),标签不受它的影响。

最后,我可能会指出,在我的机器上,我只是直接在UILabel上运行你的代码(不是在表视图或任何东西中),并且它起作用了:

[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
contentLabel.text = @"Start of a really long text to test truncation and display in activity stream. It should be truncated with ellipses. End.";

enter image description here

因此,我建议您的代码未运行或未应用于标签,或者某些其他代码正在出现并更改此标签的换行模式。对于一个非常简单的案例(比如我的,在上面的代码中),最好说服自己某些东西通常,然后试图找出为什么特定的结果似乎不同于......