-
iOS 10.3
-
使用
NSStrikethroughStyleAttributeName
添加删除线失效
添加删除线经常用在商品原价情形下,使用NSStrikethroughStyleAttributeName
添加删除线在 iOS10.3之前没什么问题,结果出来一个奇葩的版本10.3(10.3.1依然未解决这个 bug),删除线渲染不出来。
目前已知的有效解决方案如下:
方案1:(推荐)
Adding a
NSBaselineOffsetAttributeName
, as explained here, to the attributed string brings back the strikethrough line. OverridingdrawText:in:
can be slow especially on Collection View or Table View Cells.
方案2:(不推荐)
I make custom UILabel class and override drawText
function, it works forNSStrikethroughStyleAttributeName
attribute. But its not support multiple line texts in simple string
.
/// This UILabel subclass accomodates conditional fix for NSAttributedString rendering broken by Apple in iOS 10.3
final class PriceLabel: UILabel {
override func drawText(in rect: CGRect) {
guard let attributedText = attributedText else {
super.drawText(in: rect)
return
}
if #available(iOS 10.3, *) {
attributedText.draw(in: rect)
} else {
super.drawText(in: rect)
}
}
}