NSMutableParagraphStyle:某些属性不适用于iOS 10.3.2和10.3.3

时间:2023-02-01 07:56:40

Im trying to set a NSMutableParagraphStyle in the following way:

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

...which doesn't change anything about the appearance of the text on iOS 10.3.2 and .3. It works on iOS 9 and iOS 11.

...这不会改变iOS 10.3.2和.3上文本外观的任何内容。它适用于iOS 9和iOS 11。

I've added the baselineOffset which seems to solve the issue for some users (radar) - it doesn't help in my case. Is there any other workaround, or do all the users with 10.3.x now have to live with broken layouts all day?

我已经添加了baselineOffset,它似乎解决了一些用户(雷达)的问题 - 在我的情况下它没有帮助。有没有其他解决方法,或者所有10.3.x的用户现在都需要整天使用破损的布局?

EDIT: @Larme I tried this, which doesn't work either:

编辑:@Larme我试过这个,这也不起作用:

        let attributedString = NSMutableAttributedString(string: headline.text!, attributes: attributes)
        attributedString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.blue, range: NSMakeRange(0, 3))
        headline.attributedText = attributedString

EDIT: After testing matt's code on both iOS 10.3 and 11 devices, I've come to the conclusion that there's a problem when setting the line spacing and max height - setting text alignment works without restrictions. Even when not doing anything in code and just using the Xcode UI to set up my attributed text, I end up with the following result (notice the line spacing):

编辑:在iOS 10.3和11设备上测试matt的代码后,我得出的结论是,在设置行间距和最大高度时存在问题 - 设置文本对齐无限制地工作。即使没有在代码中做任何事情,只是使用Xcode UI设置我的属性文本,我最终得到以下结果(注意行间距):

NSMutableParagraphStyle:某些属性不适用于iOS 10.3.2和10.3.3

NSMutableParagraphStyle:某些属性不适用于iOS 10.3.2和10.3.3

iOS 11 Device

iOS 11设备

NSMutableParagraphStyle:某些属性不适用于iOS 10.3.2和10.3.3

iOS 10.3.3 Device

iOS 10.3.3设备

NSMutableParagraphStyle:某些属性不适用于iOS 10.3.2和10.3.3

1 个解决方案

#1


0  

No. this is definitely a bug that I see no possible way of working around of short of getting apple to patch it.

不,这绝对是一个错误,我认为没有办法解决它的问题。

I'm sorry :(

对不起 :(

#1


0  

No. this is definitely a bug that I see no possible way of working around of short of getting apple to patch it.

不,这绝对是一个错误,我认为没有办法解决它的问题。

I'm sorry :(

对不起 :(