缩进UILabel的第二行

所以我有一个UILabel,可能会或可能不会去第二行,这取决于它是在iPhone还是iPad上。 我想要完成的是在第二行上缩进,以便在需要时正确排列。

在iPad上,它几乎不需要第二次换行,并且取决于在哪个iPhone上运行,它可能会也可能不会。 所以,本质上,我需要一种方法来dynamic缩进第二行,只有当有第二行。

为标签使用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; 

结果:

示例结果