UILabel attributedText with multiple line break modes

前端 未结 4 2111
时光取名叫无心
时光取名叫无心 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;
    

提交回复
热议问题