-1

I want to change myTextField background color to the color I type into the same myTextField. Here is my method: and I also called this method in the viewDidLoad section: changeColor() but it doesn't work :(

func changeColor() {

    if myTextField.text == "blue" {
        myTextField.backgroundColor = .blue
    } else if myTextField.text == "red" {
        myTextfield.backgroundColor = .red
    } else {
        myTextField.backgroundColor = .black
    }
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField == myTextField {
        if range.location > 3 {
            return false
        }
    }
    
    return true
}
  • 2
    And what's happening? `changeColor()` is called ONLY in `viewDidLoad()`? What's the value of `myTextField.text` then? Shouldn't you want to listen to text change and call `changeColor()` there? Did you look how to do so? By implementing `UITextFieldDelegate` methods? – Larme May 31 '21 at 16:30
  • UITextFieldDelegate is implemented, i can type anything, it appears in the textField. When the app starts, the textField is empty, and its background color is white(default), and when I type "blue" or "red" nothing happens, but i want the background color to be set to blue or red – László Gábor May 31 '21 at 16:39
  • @LászlóGábor have you implemented [`shouldChangeCharactersIn`](https://stackoverflow.com/a/35631668/14351818)? – aheze May 31 '21 at 16:42
  • You don't call `changeColor()` in the corresponding `UITextFieldDelegate` method? At start, it should be black indeed, instead of white though. Could you show more code? – Larme May 31 '21 at 16:44
  • yes I have, when i set the maximum character number func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if textField == myTextField { if range.location > 3 { return false } } return true } – László Gábor May 31 '21 at 16:46
  • and it also works, i can type max 4 characters – László Gábor May 31 '21 at 16:46
  • @LászlóGábor can you [edit](https://stackoverflow.com/posts/67777368/edit) your question and show full code of `shouldChangeCharactersIn`? – aheze May 31 '21 at 16:49
  • But you don't call `changeColor()` inside that method? It's not because you called it in `viewDidLoad()` that it will be triggered each time, it's not bind with the textField. You need to call it each time you want to do so. – Larme May 31 '21 at 16:57
  • trying.... but somehow it doest show the way i wanted – László Gábor May 31 '21 at 16:58
  • ok so instead of calling "changeColor()" in the viewDidLoad, I should called it in the "shouldChangeCharactersIn" method? – László Gábor May 31 '21 at 17:00

1 Answers1

0

You could add an extension for UITextField to add the changeColor function to the class (in any part of your code, it has to be outside classes). Then make an objc function to run the function of changing color. Finally, target the objc function to the textField with .editingChanged mode in viewDidLoad().

The ViewController should look like this:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myTextField: UITextField!

    //Target function
    @objc func changeColor(_ sender:UITextField){
        sender.changeColor()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //Add target
        myTextField.addTarget(self, action: #selector(changeColor(_:)), for:      .editingChanged)
    }
}

//Extension to add function
extension UITextField{
    func changeColor() {
        if self.text == "blue" {
            self.backgroundColor = .blue
        } else if self.text == "red" {
            self.backgroundColor = .red
        } else {
            self.backgroundColor = .black
        }
    }
}
FelipeCruzV10
  • 426
  • 6
  • 13