UILabel keeps attribute 'strikethroughStyle' when setting its text property to nil

I noticed the following problem:


How to reproduce:

1. Set a NSAttributedString with strikethroughStyle and NSUnderlineStyle.single to a label's attributedText property.

2. Set the label's text property to nil.

3. Set a NSAttributedString with some attributes but without strikethroughStyle to the label's attributedText property.

4. Then the label's text is strikethrough on iOS 13 beta 6.


On iOS 12, the attributes where removed when setting the label's text property to nil.


The following demo code shows the behaviour:

import UIKit


class ViewController: UIViewController {


    lazy var label1: UILabel = {
        let result = UILabel(frame: CGRect(x: 10, y: 100, width: 300, height: 50))
        result.backgroundColor = UIColor.lightGray
        result.text = "label 1"
        return result
    }()


    lazy var label2: UILabel = {
        let result = UILabel(frame: CGRect(x: 10, y: 150, width: 300, height: 50))
        result.backgroundColor = UIColor.lightGray
        result.text = "label 2"
        return result
    }()


    lazy var button: UIButton = {
        let result = UIButton(frame: CGRect(x: 10, y: 200, width: 300, height: 50))
        result.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
        result.setTitle("PRESS ME", for: .normal)
        result.backgroundColor = .black
        return result
    }()


    static var count = 0


    override func viewDidLoad() {
        super.viewDidLoad()


        self.view.addSubview(label1)
        self.view.addSubview(label2)
        self.view.addSubview(button)
    }


    @objc
    func buttonPressed(_ button: UIButton) {


        let attrRedStrikethroughStyle = [
            NSAttributedString.Key.strikethroughStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)
        ]


        let attrBlueNoStrikethroughStyle = [
            NSAttributedString.Key.foregroundColor:  UIColor.red,
        ]


        switch  type(of: self).count%3 {
        case 0:
            let text = NSAttributedString(string: "This should be strikethrough.",
                                          attributes: attrRedStrikethroughStyle)
            label1.attributedText = text
            label2.attributedText = text
        case 1:
            label1.text = nil
            // label2.text = nil    // If this line is active the strikethroughStyle will still be shown.
                                    // However, if label2.attributedText = nil is executed fist, it will work.
            label2.attributedText = nil
        default:
            let text = NSAttributedString(string: "This should NOT be strikethrough.",
                                          attributes: attrBlueNoStrikethroughStyle)
            label1.attributedText = text
            label2.attributedText = text
        }


        type(of: self).count += 1
    }


    /*
     How to reproduce:
     1. Set a NSAttributedString with strikethroughStyle and NSUnderlineStyle.single to a label's attributedText property.
     2. Set the label's text property to nil.
     3. Set a NSAttributedString with some attributes but without strikethroughStyle to the label's attributedText property.


     Then the label's text is strikethrough on iOS 13 beta 6. On iOS 12 this was not the case.




     The example code shows the two different behaviors.
    */


}

Is this something that will be fixed in the release of iOS 13?

Replies

Does anyone have news on this issue? I'm experience something very similar. Old attributes are kept in the label even after creating and setting a new attributed string without any attributes at all.

Having the same issue on 13.2 and 13.3

The fact is that in this line of code - label1.text = nil or label2.text = nil - you do not remove the attributedText, so the strikethroughStyle remains.