微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/iOS/335240.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐