UILabel多线截断中间整个字符串不起作用

时间:2017-07-06 10:34:15

标签: ios objective-c uilabel line-breaks truncation

我使用的是UILabel,共有2行。 我想让它像

"Had the best experience ever! She is very gener... – Mary"截断中间部分,但我无法达到相同的结果。

还要考虑label必须是2行,并且最后的名称必须出现。 我的label初始化代码如下

    self.labelMessage = [[VTUIKit labelSecondary] addToSuperview:self];
    self.labelMessage.numberOfLines = 2;
    self.labelMessage.font = kVTFontSecondaryVeryTiny;
    self.labelMessage.textColor = kVTColorGrey300;
    self.labelMessage.lineBreakMode = NSLineBreakByTruncatingMiddle;
    self.labelMessage.f_width = [VTReviewDisplayCell cellSize].width - 16;

设置label文字

    NSString *reviewString = [NSString stringWithFormat:@"%@ - %@", self.model.message, self.model.customer_user.first_name];
    self.labelMessage.text = reviewString;

我不明白为什么它不能正常工作。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码

UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(40,100, 250, 60)];
    contentLabel.numberOfLines = 2;
    contentLabel.text = @"Had the best experience ever! She is very generally uwuq wuqe iwqwq – Mary";
    contentLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [self.view addSubview:contentLabel];

enter image description here