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