iOS多行标签NSLineBreakByTruncingTail

iOS多行标签NSLineBreakByTruncingTail,ios,uilabel,Ios,Uilabel,我有一个标签,numberOfLines设置为2,lineBreakMode设置为nslinebreakbytruncingtail。我原以为当文本超过两行时,标签会用省略号将其截断。但是现在发生的是,尽管文本被截断,但没有显示省略号 为什么会这样?谢谢 更多详情: 以下是UITableViewCell的自定义类,其中包含有问题的标签(contentLabel)。标签在loadConversation方法中更新 @interface CPActivityStreamCell () @proper

我有一个标签,
numberOfLines
设置为2,
lineBreakMode
设置为
nslinebreakbytruncingtail
。我原以为当文本超过两行时,标签会用省略号将其截断。但是现在发生的是,尽管文本被截断,但没有显示省略号

为什么会这样?谢谢

更多详情: 以下是UITableViewCell的自定义类,其中包含有问题的标签(
contentLabel
)。标签在
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中的相同标签:

输出: 顶部单元格的contentLabel设置为真正长文本的
开始,以测试截断并显示在活动流中。它应该用椭圆截断。完。


问题存在于初始化过程的架构中,或者可能存在于您在其他地方正在做的事情中(在您的问题中没有显示的代码中)。你是说

[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
但是这个代码并没有应用到我们在屏幕截图中看到的标签上。这一点很清楚,因为如果应用它,文本将是浅灰色

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

最后,我可以指出,在我的机器上,我只是直接在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.";


因此,我建议您的代码未运行或未应用于标签,或者其他一些代码正在出现并更改此标签的换行模式。最好让自己相信某些东西通常适用于非常简单的情况(如我的,在前面的代码中),然后试着找出为什么特定结果似乎与此不同……

它应该有效,但只有在设置标签的
文本时才有效。如果您正在设置
属性文本
,则所有下注都将被取消。你能提供这个标签配置的更多细节吗?Thx.您可以通过调试活动对象来检查标签的linbreakMode吗?还可以检查标签的框架。我已调试,调试显示标签的numberOfLines属性为2,lineBreakMode为4。关于颜色。在loadConversation方法中将颜色重置为黑色。当对话有图像时,它被设置为白色。代码很乱,我正在清理。是的,很抱歉;我终于注意到了关于颜色的问题。不过,我已经向您证明了,通过向您显示代码和屏幕截图,这可以正常工作。因此,现在你必须弄清楚为什么你在应用程序中看到的标签不会出现这种情况。这是我代码中的一个错误:(.conversation.content在CPConversation类的实现中被截断为75个字符。做得好!请注意我们在这里使用的技术:通过完全脱离应用程序的上下文执行相同的操作,我们证明了一定有其他代码在起作用,因为否则它会按预期工作.当我陷入困境时,我经常使用这个技巧:制作一个小型项目,在真实应用程序的复杂性之外,测试事物是如何独立工作的。