缩进 UILabel 的第二行
Indent second line of UILabel
所以我有一个 UILabel,它可能会或可能不会转到第二行,具体取决于它是在 iPhone 还是 iPad 上。我想要完成的是让它在第二行缩进以正确排列,如果需要的话。
在 iPad 上,它几乎不需要第二个换行符,并且取决于 iPhone 它在 运行 上,它可能会也可能不会。所以,本质上,我需要一种动态缩进第二行的方法,只有当有第二行时。
使用 NSAttributedString
作为标签,并设置 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;
结果:
所以我有一个 UILabel,它可能会或可能不会转到第二行,具体取决于它是在 iPhone 还是 iPad 上。我想要完成的是让它在第二行缩进以正确排列,如果需要的话。
在 iPad 上,它几乎不需要第二个换行符,并且取决于 iPhone 它在 运行 上,它可能会也可能不会。所以,本质上,我需要一种动态缩进第二行的方法,只有当有第二行时。
使用 NSAttributedString
作为标签,并设置 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;
结果: