Indent second line of UILabel

后端 未结 1 1852
情歌与酒
情歌与酒 2021-01-12 07:09

So I have a UILabel that may or may not go to a second line, depending if it is on iPhone or iPad. What I would like to accomplish is to have it indent on the second line to

1条回答
  •  再見小時候
    2021-01-12 08:01

    Use an NSAttributedString for your label, and set the headIndent of its paragraph style:

    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.headIndent = 14;
    NSDictionary *attributes = @{
        NSParagraphStyleAttributeName: style
    };
    NSAttributedString *richText = [[NSAttributedString alloc] initWithString:@"So this UILabel walks into a bar…" attributes:attributes];
    self.narrowLabel.attributedText = richText;
    self.wideLabel.attributedText = richText;
    

    Result:

    example result

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