缩进UILabel的第二行 [英] Indent second line of UILabel
问题描述
所以我有一个可能会或可能不会进入第二行的UILabel,具体取决于它是在iPhone还是iPad上。我想要完成的是在第二行缩进以正确排列,如果需要的话。
So I have a UILabel that may or may not go to a second line, depending if it is on iPhone or iPad. What I would like to accomplish is to have it indent on the second line to line up correctly, if needed.
在iPad上它几乎不需要第二次换行,并且根据它运行的iPhone,它可能会也可能不会。所以,实质上,我需要一种动态缩进第二行的方法,只有当有第二行时。
On iPad it will almost never need the second line break, and depending on which iPhone it is running on, it may or may not. So, in essence, I need a way to dynamically indent the second line, only when there is a second line.
推荐答案
使用标签的 NSAttributedString
,并设置 headIndent
.apple.com / library / ios / documentation / UIKit / Reference / NSAttributedString_UIKit_Additions /#// apple_ref / c / data / NSParagraphStyleAttributeName>段落样式:
Use an NSAttributedString
for your label, and set the headIndent
of its 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;
结果:
这篇关于缩进UILabel的第二行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!