0

I'm trying to change the colour of text being typed to white in UITextfield. I've used following code, which works but it doesn't change the colour of first letter to white. See the attached screenshot.

How to make it work so that colour of first letter also changes to white?

I've searched a lot but couldn't find the solution.

Tx in advance!

enter image description here

@IBAction func emailFieldEditingChanged(_ sender: UITextField) {

    emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
}
Matt
  • 315
  • 2
  • 6
  • 20

1 Answers1

1

You'll want to set the delegate as Rakesha mentioned, but you have to change it each time the user inputs a character. Use this delegate method.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
    return true
}

//Set this in viewDidLoad, and don't forget to include the delegate.
emailTextField.delegate = self
Derek
  • 1,011
  • 6
  • 17