Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to turn auto-correction on/off based on the content in the text field.

For example, if the user is typing a phrase like "Machine tools" I want auto-correction to be on (for the rest of the words she's gonna type) but if I sense the beginnings of - say - a web address like "www.mach.." I want to turn auto-correction off.

I tried doing this on receiving the UITextFieldTextDidChangeNotification, by toggling autoCorrectionType on the textfield based on the content typed. While the actual value of this property on the text field changes (verified this with an NSLog) the actual correction behaviour does not get affected. So if auto-correction was on at the beginning of the editing session, it continues to be so even AFTER I have set the text field to UITextAutoCorrectionTypeNo. So "www.foogle..." gets corrected to "www.google.." which may not always be desirable in my book.

So has anyone found a way to enable/disable auto-correction on the fly (when editing) in a text field?

Thanks.

I have no experience trying to do this. Suggestions though: 1) have you tested on device and not just in simulator? Might be diff. there. 2) Though ugly, you might be able to use a pair of text fields with two different settings for that value, and more or less seamlessly transition between the two, hiding the one not used and updating text/first responder status for the visible one. wkw May 5, 2010 at 15:51

UITextView has to reload something internally which it doesn't do automatically, so we have to trigger it from the outside. A way I found is to resign and become first responder again:

[textView resignFirstResponder];
// UITextAutoCorrectionType change
[textView becomeFirstResponder];

This works. Remember to restore the selected range.

Raphael

That sounds about right. Thanks! @wkw, apologies for not responding to your comment earlier. Somehow didn't get an email notification of a response to my query. Thanks for your inputs. Cheers, Dev – Dev Kanchen Jul 14, 2010 at 14:29

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.