Set UILabel line spacing

后端 未结 10 1561
鱼传尺愫
鱼传尺愫 2020-11-27 10:47

How can I modify the gap between lines (line spacing) in a multiline UILabel?

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 11:00

    Of course, Mike's answer doesn't work if you pass the string programmatically. In this case you need to pass a attributed string and change it's style.

    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"Your \nregular \nstring"];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:4];
    [attrString addAttribute:NSParagraphStyleAttributeName
                       value:style
                       range:NSMakeRange(0, attrString.length)];
    _label.attributedText = attrString;
    

提交回复
热议问题