Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot change the variable

Hello guys I’m creating a custom text label and I can’t change labels text from viewcontroller so I need help.

There is codes from my custom text labels swift file:

import UIKit

class LinkLabel: UILabel {
    
    private var labelFirstText: String? = "First Text"
    private var labelSecondText: String? = "Second Text"
    
    var firstLabel: String? {
        didSet {
            self.labelFirstText = firstLabel
        }
    }
    var secondLabel: String? {
        didSet {
            self.labelSecondText = secondLabel
        }
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupLabel()
        self.setNeedsDisplay()
    }
    
    @objc
    private func setupLabel() {
        let firstTextNSA = [NSAttributedString.Key.font:
                                UIFont.systemFont(ofSize: 15, weight: .medium),
                            NSAttributedString.Key.foregroundColor: UIColor.secondaryTextColor]
        let secondTextNSA = [NSAttributedString.Key.font:
                                UIFont.systemFont(ofSize: 15, weight: .medium),
                             NSAttributedString.Key.foregroundColor: UIColor.appPurple]
        let attributedString1 = NSMutableAttributedString(string: labelFirstText ?? "First Label" + " ", attributes: firstTextNSA)
        let attributedString2 = NSMutableAttributedString(string: labelSecondText ?? "Second Label", attributes: secondTextNSA)

        attributedString1.append(attributedString2)
        self.attributedText = attributedString1
    }

}

And there is my viewcontroller:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet private weak var signInLabel: LinkLabel!
    
    // MARK: - Life Cycle
        override func viewDidLoad() {
            super.viewDidLoad()
            signInLabel.firstLabel = "Already have an account?"
            signInLabel.secondLabel = "Sign in now"
        }
}

Everything is work right now but just I can’t change the label text. I write print functions for log what happen and see when didset run, the labelFirstText is "Already have an account?" but when init function run labelFirstText take default value. I don’t understand how can I fix that.

>Solution :

Add a call to setupLabel() immediately after any of the values are changed:

var firstLabel: String? {
    didSet {
        self.labelFirstText = firstLabel
        setupLabel()
    }
}

var secondLabel: String? {
    didSet {
        self.labelSecondText = secondLabel
        setupLabel()
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading