【问题标题】:UILabel multiline truncate middle as a whole string not workingUILabel 多行截断中间作为一个整体字符串不起作用
【发布时间】:2017-12-10 07:40:14
【问题描述】:

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

"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;

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

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    试试下面的代码

    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];
    

    【讨论】:

    • 谢谢,但这不起作用。我正在添加标签以首先查看并通过NSString *reviewString = [NSString stringWithFormat:@"%@ - %@", self.model.message, self.model.customer_user.first_name]; self.labelMessage.text = reviewString; 设置其文本,这会有什么不同吗?
    • 你的屏幕尺寸、标签宽度和高度是多少?
    • 我按照您的建议将高度设置为 60,宽度为每个屏幕尺寸固定为 151,我支持所有 iphone 屏幕。
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    相关资源
    最近更新 更多