1

I want to display a string in a UILabel, where part of it has a strikethrough style. It seems I can't put the strikethrough attribute on only part of the string. If it's on the whole string, it displays; but if it's on part of the string, it does not. Is this a bug? Am I missing something?

Here is some example code, and a screen shot.

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let strikeAttrs = [NSStrikethroughStyleAttributeName: 
                           NSUnderlineStyle.styleSingle.rawValue]

        let str1 = NSMutableAttributedString()
        str1.append(NSAttributedString(string: "aaa", attributes: strikeAttrs))

        let str2 = NSMutableAttributedString()
        str2.append(NSAttributedString(string: "aaa", attributes: nil))
        str2.append(NSAttributedString(string: "bbb", attributes: strikeAttrs))

        label1.attributedText = str1
        label2.attributedText = str2
    }

}

The screen looks like this:

enter image description here

Rob N
  • 15,024
  • 17
  • 92
  • 165
  • Hii Rob, your code is work perfect fro me – jignesh Vadadoriya Apr 06 '17 at 04:36
  • The code attached seems to give correct output as expected. Did you try on a device? – firstinq Apr 06 '17 at 06:27
  • My device gives same result as the simulator screenshot. It is also running 10.3. Let me see if I can find some other versions of iOS to test. What version of iOS did you guys use? – Rob N Apr 06 '17 at 13:01
  • Yup, iOS 9.3 simulator does correctly draw strikethrough line on the "bbb" part of the second string. – Rob N Apr 06 '17 at 13:04
  • This is a bug in iOS 10.3, see also here: http://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub/43359207 – koen Apr 23 '17 at 16:36

0 Answers0