首页 > 解决方案 > strokeThrough 属性与 iOS 13 不兼容

问题描述

我已使用此String扩展程序将罢工添加到文本中

这是扩展名:

extension String{
func strikeThrough()->NSAttributedString{
    let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: self)
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length))
    return attributeString
}

在这里我如何使用UILabel

     if isDone {
            self.title?.attributedText = title.strikeThrough()
        } else {
            self.title?.attributedText = nil
            self.title?.text = title
        }

它在 iOS 12 中运行良好,但在 iOS 13 中,self.title?.attributedText = nil不会删除删除线。在我的应用程序中,当在文本上添加罢工时,它将从列表中删除,但现在,它已被删除,但罢工将出现在其他文本上,这不应该发生。还有self.title?也不为零。

您能否建议我以任何方式strikeThrough从文本中删除,当前的表单nil不起作用。

太感谢了

标签: swiftios13

解决方案


在我不想再显示的情况下,我通过将值设置为 0 来使其工作。

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: weightString)

//HACK we have to set strikethrough to 0 for ios 13 or it won't remove the strike even if setting the attributed text to nil

attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 0, range: NSMakeRange(0, attributeString.length))

cell.weightLabel.attributedText = attributeString))


推荐阅读