How to allow only certain characters in UITextView?
I want to allow only English characters & numbers & Arabic characters.
I found many solutions but for UITextField not UITextView!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
textView:shouldChangeTextInRange:replacementText:method of theUITextViewDelegate(docs):You should check if the
replacementTextparameter contains illegal characters. In that case you can returnNOto prevent the text from being inserted. This might give problems when a text of more than one character is inserted, and not all characters are allowed: what do you do, allow the insertion (including the illegal characters) or prevent it (including the allowed characters)?This means you need the
textViewDidChange:method (docs):When this method is called you could check the value of the
UITextViewand remove illegal characters afterwards.I think it would be best to use both methods. The first one gives the best experience when the user is typing (the character is not displayed at all, instead of first being displayed and later being removed). The second method makes sure that illegal characters will be removed if some text is pasted, for example.