0

I have a UITextField where I need to replace certain characters the user may type with different ones (for example, I need to append a suffix to the text that has been entered by the user if he has tapped a space), and show the result to the user in such UITextField.

I'm trying to directly set textField.text = "my text" when I am notified that the text in the text field has changed, but it is not working. In the examples I have found, the text in the text field is not changed, it is only checked for certain characters without modifying texField.text

How could I change the text in a text field in code, and show there the result?

AppsDev
  • 12,319
  • 23
  • 93
  • 186

2 Answers2

0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
    if string == " "
    {
        textField.text?.append("q")
    }
    return true
}

Add UITextFieldDelegate to your ViewController and

textField.delegate = self
Developer
  • 822
  • 9
  • 23
0

What I understand from you question.

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

Check this link for more

You will get updated textfield text in this function. If you find anything char which you want to change replace or append anything here.

Mathi Arasan
  • 869
  • 2
  • 10
  • 32