UILabel 如何缩进第二行?

11
我有一个UILabel,根据它在iPhone或iPad上的显示情况,可能会换到第二行。如果需要,我希望实现的是在第二行缩进,以使其正确对齐。
在iPad上,它几乎永远不需要换行到第二行,但根据运行的iPhone型号可能需要或不需要。因此,本质上,我需要一种方法仅在存在第二行时动态缩进第二行。
1个回答

29

使用NSAttributedString来设置标签,并设置其段落样式headIndent

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;

结果:

示例结果


我在使用自定义字体时遇到了问题。你也有这个问题吗? - Klemen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接