我正在尝试通过以下方式设置 NSMutableParagraphStyle
:
@IBOutlet weak var headline: UILabel! {
didSet {
let style = NSMutableParagraphStyle()
style.maximumLineHeight = 15
style.lineSpacing = 0
style.alignment = .center
let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey.paragraphStyle : style,
NSAttributedStringKey.baselineOffset : 0]
let attributedString = NSMutableAttributedString(string: headline.text!, attributes: attributes)
headline.attributedText = attributedString
}
}
...在 iOS 10.3.2 和 .3 上不会改变文本的外观。它适用于 iOS 9 和 iOS 11。
我添加了 baselineOffset
,它似乎可以解决某些用户的问题 (radar) - 它对我的情况没有帮助。是否有任何其他解决方法,或者所有使用 10.3.x 的用户现在都必须整天忍受损坏的布局?
编辑: @Larme 我试过这个,它也不起作用:
let attributedString = NSMutableAttributedString(string: headline.text!, attributes: attributes)
attributedString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.blue, range: NSMakeRange(0, 3))
headline.attributedText = attributedString
编辑:在 iOS 10.3 和 11 设备上测试 matt 的代码后,我得出的结论是设置行距和最大高度时存在问题 - 设置文本对齐可以不受限制地工作。即使没有在代码中做任何事情,只是使用 Xcode UI 来设置我的属性文本,我最终也会得到以下结果(注意行间距):
iOS 11 设备
iOS 10.3.3 设备
最佳答案
没有。这绝对是一个错误,我认为除了让 Apple 对其进行修补外,没有其他可能的解决方法。
对不起:(
关于ios - NSMutableParagraphStyle : Some properties not working on iOS 10. 3.2 和 10.3.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49735263/