ios - 缩进 UILabel 的第二行

标签 ios objective-c uilabel

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

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

最佳答案

使用 NSAttributedString 作为标签,并设置 headIndent它的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;

结果:

example result

关于ios - 缩进 UILabel 的第二行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990173/

相关文章:

ios - ITMS-90809:不推荐使用的API-Apple将停止接受使用UIWebView API的应用程序的提交

objective-c - 无法从自定义类获取数据

ios - 当键盘出现在 iOS (Cordova) 上时如何调整 Web View 的大小

iphone - 在 UIView 中逐步绘制 (iPhone)

iphone - 如何从 UILabel 创建图像?

ios - 如何在 swift 中使用 NSUserDefaults 保存标签?

ios - 在 iOS 中使用 NSDictionary 的可扩展 UITableview 单元格

iphone - 在 UIImageView 上添加不透明度为 0.3 的黑色叠加层

ios - 标签显示 html 数据并使图像和链接可快速点击

cocoa - 为什么将 uilabel 添加到多个 subview 不起作用?