【问题标题】:Justify text for UILabel in iOS 9在 iOS 9 中为 UILabel 对齐文本
【发布时间】:2015-11-02 10:18:00
【问题描述】:

我找到了通过属性字符串在 UILabel 中为 iOS 8.4 以下版本证明文本的解决方案:将标签字符串设置为属性并修改连字符值,如下所示。

此解决方案在 iOS 9 上停止工作(文本显示为左对齐)。我需要其他支持 iOS 7 或至少适用于 iOS 9 的工作解决方案(会在某处添加“if”)

【问题讨论】:

  • 也许它被删除了,因为从排版上讲,证明行尾是正确的做法被认为是不好的做法。
  • NSAttributedString 适用于 ios 7,您可以从 here 获得更多信息
  • @NghiaLuong,它归因于工作构建,但在 iOS 9 上,理由不再适用。它只对我有效吗?
  • @NghiaLuong,阅读您的链接给了我一个想法,将我的标签转换为 textViews,允许从笔尖证明文本的正确性。这不是快速修复,但至少它有效。谢谢!顺便说一句,任何希望 UITextView 自动计算其内在大小(自动布局)的人 - 关闭滚动

标签: objective-c uilabel ios9 justify


【解决方案1】:

更新:

似乎将@"locale" 键添加到NSAttributedString 的属性可能会有所帮助。见here

原答案:

您可以随时在这里使用NSAttributedString

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1;
paragraphStyle.alignment = NSTextAlignmentJustified;

NSDictionary *attributes = @{
    NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody],
    NSParagraphStyleAttributeName: paragraphStyle
};

NSString *string = @"your text here";

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];

self.label.attributedText = attributedString;
self.label.numberOfLines = 0;

【讨论】:

  • 我使用截图中提供的属性字符串。唯一的区别是我在 XIB 中做到了这一点,而不是在代码中。但正如我在问题中提到的 - 它在 iOS 9 中不起作用。它显示为左对齐
  • @PooLaS 有趣的是,当我以我的本地语言(俄语)提供文本时,它看起来是合理的和带连字符的,但在英语中它是左对齐且不带连字符的。
猜你喜欢
  • 1970-01-01
  • 2012-10-07
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多