我试图以下列方式设置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设备