views:

143

answers:

1

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.

A: 

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

Raphael Schaad
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