2

Im new in this... I have one text field and one label... I would like to change label color in case when message in text field and label text are not equal. This is my start:

@IBAction func tapMeButton(_ sender: Any) {

    label.text = txtField.text

    }

How can I do this?

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Sonya Kovach
  • 49
  • 3
  • 8
  • `UILabel`s have a `textColor` field, which takes a `UIColor`. You can use an `if` statement to determine if the messages are the same (you might want to set both strings to lowercase, if you don't care about capitalisation). – Rich Tolley Jan 26 '18 at 10:09

4 Answers4

2

Add this line to viewDidLoad

  textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

and

@objc func textFieldDidChange(_ textField: UITextField) {

   if(label.text != txtField.text)
  {
      label.textColor = UIColor.red
  }

}

@IBAction func tapMeButton(_ sender: Any) {

    label.text = txtField.text
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thanks for answer. But I want to change color when I change text. In the begining my text field and my label are same when I press the button. And then, when I want to change label color when I write new text in text field. – Sonya Kovach Jan 26 '18 at 10:11
1

UITextField provide delegate method, you can use this code to check both Value are same or not.

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let nsString = textField.text as NSString?
        let newString = nsString?.replacingCharacters(in: range, with: string)

        if(self.lbl.text != newString) {
             self.lbl.textColor = UIColor.red
        }
        else {
             self.lbl.textColor = UIColor.green
        }

        return true;
     }
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
0

First add target for getting the change of text.

textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

func textFieldDidChange(_ textField: UITextField) {
    if(label.text != textField.text)
 {
   label.textColor = UIColor.red
 }
}

You can also see this: https://stackoverflow.com/a/28395000/5167909

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

To do this you have to understand the the textfield more,

  1. Every textfield comes with delegate method to do various functionalities around it. so first in your viewDidLoad method, add delegate to it like this

    yourTextField.delegate = self
    
  2. Add the delegate methods by assigning textFieldDelegate to your class by adding its extension. It will provide you text at real time, while user starts typing and before even get added to textField

    extension YourViewController:UITextFieldDelegate {
    
    
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    
        if let text = textField.text as NSString? {
            let txtAfterUpdate = text.replacingCharacters(in: range, with: string)
    
            if txtAfterUpdate == label.text {
    
                label.backgroundColor = UIColor.red
    
            } else {
    
                label.backgroundColor = UIColor.black
                }
    
    
            }
            return true
    
        }
    
    
    
    }
    
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sucharu Hasija
  • 1,096
  • 11
  • 23