【问题标题】:UILabel with multiple chained NSAttributedString, with line limit, show the tail truncation with NSBackgroundColorAttributeName of unseen text带有多个链接的 NSAttributedString 的 UILabel,带有行限制,显示尾部截断,带有未见文本的 NSBackgroundColorAttributeName
【发布时间】:2016-01-05 15:46:12
【问题描述】:

点是由UILabel自动添加的,导致行限制,但它们获得了隐藏截断文本的背景颜色:

所以我有 UILabel,行数限制为 10,换行模式为 TruncateTail。 我还有 2 种类型的属性字符串来构建这个 UILabel 内容。

  1. NSForegroundColorAttributeName、NSFontAttributeName
  2. NSBackgroundColorAttributeName、NSForegroundColorAttributeName、NSFontAttributeName

知道为什么 UILabel 会为点添加背景颜色吗?第 12 行(被截断)中有具有该背景的文本...

【问题讨论】:

  • 您能添加 NSAttributedString 代码吗?会帮助我理解:)
  • 截断文本中是否存在“Windsor”字符串的实例?
  • 是的,截断的文本中有一个 Winsor 的实例。

标签: ios objective-c iphone uilabel nsattributedstring


【解决方案1】:

=这里给定代码试试看..

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"test"];

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];

style.lineBreakMode = NSLineBreakByTruncatingTail;

[文本 addAttribute:NSParagraphStyleAttributeName 价值:风格 range:NSMakeRange(0, text.length)];

label.attributedText = 文本;

【讨论】:

    【解决方案2】:

    看起来您的 NSAttributedString 样式正在接受超过截断点的“Windsor”实例。

    您可以找到截断发生的位置,然后仅将文本属性应用于字符串的范围直到截断点。

    请参阅this SO answer 以计算此范围。

    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineBreakMode = mylabel.lineBreakMode;
    NSDictionary *attributes = @{NSFontAttributeName : mylabel.font,
                                 NSParagraphStyleAttributeName : paragraph};
    CGSize constrainedSize = CGSizeMake(mylabel.bounds.size.width, NSIntegerMax);
    CGRect rect = [mylabel.text boundingRectWithSize:constrainedSize
                                             options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                          attributes:attributes context:nil];
    if (rect.size.height > mylabel.bounds.size.height) {
        NSLog(@"TOO MUCH");
    }
    

    【讨论】:

    • 好主意。唯一的问题是标签大小一直在变化,所以我必须进行太多次计算,而且成本很高。如您所见,它确实选择了背景颜色,但没有选择字体颜色。这闻起来像一个错误,我想知道是否有人设法弄清楚
    • 这看起来像一个错误,所以我只是用不同的文本颜色而不是前景色突出显示文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    相关资源
    最近更新 更多