4

这是我用字符串扩展编写的函数。它将返回一个属性字符串。它没有在我的 UILabel 上呈现属性文本。

func strikeThroughLine(color: UIColor, textFont: UIFont, textColor: UIColor) -> NSAttributedString {
    let attributeString = NSMutableAttributedString(string: self)
    let range = NSMakeRange(0, attributeString.length)
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: range)
    attributeString.addAttribute(NSStrikethroughColorAttributeName, value: color, range: range)
    attributeString.addAttribute(NSFontAttributeName, value: textFont, range: range)
    attributeString.addAttribute(NSForegroundColorAttributeName, value: textColor, range: range)
    return attributeString
}

我制作了自定义 UILabel 类和覆盖drawText函数,它适用于NSStrikethroughStyleAttributeName属性。但它不支持简单的多行文本string

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)
    }
}
4

0 回答 0