UILabel attributedText with multiple line break modes

前端 未结 4 2097
时光取名叫无心
时光取名叫无心 2021-02-05 18:35

I have a requirement of showing a UILabel with text that has two different styles (different colours, parts of the text bolded). This is solved easily enough by using the attrib

相关标签:
4条回答
  • 2021-02-05 19:08

    I missed truncation when I set linespacing, but all I had to to was add linebreakmode to paragraphstyle

    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrahStyle setLineSpacing:1.5];
    [paragrahStyle setLineBreakMode:NSLineBreakByTruncatingTail];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"Long string that truncates"];
    [attributedText addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [attributedText length])];
    
    self.label.attributedText = attributedText;
    
    0 讨论(0)
  • 2021-02-05 19:14

    You can set up an NSParagraphStyle with any lineBreakMode you please, and apply it to the string using NSParagraphStyleAttributeName. I don't know if all of the values of NSLineBreakMode are supported, but I have no reason to believe they aren't.

    0 讨论(0)
  • 2021-02-05 19:30

    try this:

    [_text drawWithRect:_textRect options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil];

    0 讨论(0)
  • 2021-02-05 19:31

    They only way I've been able to get this to work is to not set a paragraph style.

    0 讨论(0)
提交回复
热议问题