IOS 10.3: NSStrikethroughStyleAttributeName not showing

This is my function written in a line extension. It will return an attributed string. Its not related text to my 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
}

I create a custom UILabel class and override the function drawText, it works for the attribute NSStrikethroughStyleAttributeName. But it does not support multiple lines of text in simple ones 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
source share

Source: https://habr.com/ru/post/1673580/


All Articles