It's my function written in String extension. It will return an attributted string. Its not rendering attributed text on 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 make custom UILabel class and override drawText
function, it works for NSStrikethroughStyleAttributeName
attribute. But its not support multiple line texts in simple 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)
}
}