ios – 缩进UILabel的第二行

所以我有一个可能会或可能不会进入第二行的UILabel,具体取决于它是在iPhone还是iPad上.我想要完成的是在第二行缩进以正确排列,如果需要的话.

在iPad上几乎不需要第二次换行,并且根据它运行的iPhone,它可能会也可能不会.所以,实质上,我需要一种动态缩进第二行的方法,只有当有第二行时才需要.

解决方法

为您的标签使用NSAttributedString,并设置其 paragraph style的 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;

结果:

以上是来客网为你收集整理的ios – 缩进UILabel的第二行全部内容,希望文章能够帮你解决ios – 缩进UILabel的第二行所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。