4

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)
    }
}
  • 1
    Its work in iOS 10.3 for single line of texts. Not working for multiple lines of string. – Vipul Patel Apr 05 '17 at 06:14
  • 2
    Need to add NSBaselineOffsetAttributeName attributeString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: range) It will work for multiple lines also – Piyush May 31 '17 at 06:05
  • 1
    You can check answer https://stackoverflow.com/questions/43070335/nsstrikethroughstyleattributename-how-to-strike-out-the-string-in-ios-10-3/44276985#44276985 – Piyush May 31 '17 at 06:35

0 Answers0