I want to change the background color of the view, while the text field has the Special characters like 123!@%^&()
etc otherwise background colors should be the same color.
I have implemented but changing according to the each character it should not be like this if in entire text field any special character means color has to change. Here is my code.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let specialCharacters = "!~`@#$%^&*-+();:={}[],.<>?\\/\"\'"
let searchText = textField.text! + string
//
let character = CharacterSet(charactersIn: specialCharacters)
if (string.rangeOfCharacter(from: character) != nil){
print("matched")
self.view.backgroundColor = UIColor.gray
}else
{
self.view.backgroundColor = UIColor.white
}