ios – 缩进UILabel的第二行

前端之家收集整理的这篇文章主要介绍了ios – 缩进UILabel的第二行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有一个可能会或可能不会进入第二行的UILabel,具体取决于它是在iPhone还是iPad上.我想要完成的是在第二行缩进以正确排列,如果需要的话.

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

解决方法

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

结果:

原文链接:https://www.f2er.com/iOS/335240.html

猜你在找的iOS相关文章