我试图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
}
}
Run Code Online (Sandbox Code Playgroud)
...这不会改变 iOS 10.3.2和.3上文本外观的任何内容.它适用于iOS 9和iOS 11.
我添加了baselineOffset这似乎解决了一些用户(雷达)的问题 - 它对我的情况没有帮助.还有其他任何解决方法,或者所有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
Run Code Online (Sandbox Code Playgroud)
编辑:在iOS 10.3和11设备上测试matt的代码后,我得出结论,设置行间距和最大高度时存在问题 - 设置文本对齐无限制地工作.即使没有在代码中做任何事情,只是使用Xcode UI设置我的属性文本,我最终得到以下结果(注意行间距):
iOS 11设备
iOS 10.3.3设备
| 归档时间: |
|
| 查看次数: |
447 次 |
| 最近记录: |