所以我有一个 UILabel,它可能会或可能不会转到第二行,具体取决于它是在 iPhone 还是 iPad 上。我想要完成的是让它在第二行缩进以正确排列,如果需要的话。
在 iPad 上,它几乎不需要第二个换行符,并且取决于它在哪个 iPhone 上运行,它可能会也可能不会。所以,本质上,我需要一种方法来动态缩进第二行,只有当有第二行时。
最佳答案
使用 NSAttributedString
作为标签,并设置 headIndent
它的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;
结果:
关于ios - 缩进 UILabel 的第二行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990173/