0

I've set up a UILabel programmatically and I'm attempting to change the text attribute via a function I call later on in the ViewController however when that function is called the questionLabel.text stays the default value "Welcome".

Essentially what I'm trying to accomplish is:

  func changeLabelText() {
            questionLabel.text = "New label text"
            print(questionLabel.text!)
        }
        changeLabelText()

        // prints "New label text"

however what I'm actually getting is:

   func changeLabelText() {
            questionLabel.text = "New label text"
            print(questionLabel.text!)
        }
        changeLabelText()

        // prints "Welcome"

This is how my label is setup:

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

    @IBOutlet var cameraView: UIView!

        var questionLabel: UILabel {

            let label = UILabel()
            label.lineBreakMode = .byWordWrapping
            label.backgroundColor = .white
            label.textColor = .black
            label.text = "Welcome"
            label.textAlignment = .center
            label.frame = CGRect(x: 65, y: 100, width: 300, height: 65)

            return label

        } 

Any suggestions? Greatly appreciated!

0

3 Answers 3

0

The current

var questionLabel: UILabel { 
        let label = UILabel()
        label.lineBreakMode = .byWordWrapping
        label.backgroundColor = .white
        label.textColor = .black
        label.text = "Welcome"
        label.textAlignment = .center
        label.frame = CGRect(x: 65, y: 100, width: 300, height: 65) 
        return label 
} 

is a computed property so every access gets a new separate instance

questionLabel.text = "New label text" // instance 1
print(questionLabel.text!)   // instance 2

instead you need a closure

var questionLabel: UILabel = { 
        let label = UILabel()
        label.lineBreakMode = .byWordWrapping
        label.backgroundColor = .white
        label.textColor = .black
        label.text = "Welcome"
        label.textAlignment = .center
        label.frame = CGRect(x: 65, y: 100, width: 300, height: 65) 
        return label 
}()
Sign up to request clarification or add additional context in comments.

Comments

0

Change your computed variable to a lazy initializer like so:


lazy var questionLabel: UILabel = {

    let label = UILabel()
    label.lineBreakMode = .byWordWrapping
    label.backgroundColor = .white
    label.textColor = .black
    label.text = "Welcome"
    label.textAlignment = .center
    label.frame = CGRect(x: 65, y: 100, width: 300, height: 65)

    return label

}()

Comments

0

Klamont, You can try this.

Suppose you want to change the some text of your label you always create two labels for that but it's a wrong approach of changing text color of label. You can use the NSMutableAttributedString for changing the some text color of your label.Firstly, you have to find the the range of text, which you want to change the color of that text and then set the range of your text to the NSMutableAttributedString object as compared to full string and then set your label attributedText with the NSMutableAttributedString object.

Example:

let strNumber: NSString = "Hello Test" as NSString // you must set your  
let range = (strNumber).range(of: "Test")
let attribute = NSMutableAttributedString.init(string: strNumber)
attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)
yourLabel.attributedText = attribute

If you want to use this in many times in your application you can just create the extension of the UILabel and it will make more simple :-

extension UILabel {
    func halfTextColorChange (fullText : String , changeText : String ) {
        let strNumber: NSString = fullText as NSString
        let range = (strNumber).range(of: changeText)
        let attribute = NSMutableAttributedString.init(string: fullText)
        attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)
        self.attributedText = attribute
    }
}

Use your label:-

yourLabel = "Hello Test"
yourLabel.halfTextColorChange(fullText: totalLabel.text!, changeText: "Test")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.