开发者

Toggling UITextAutoCorrectionType WHEN a UITextField is being edited

开发者 https://www.devze.com 2022-12-29 00:48 出处:网络
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 w

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 t开发者_StackOverflowo 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.


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


I was able to run [textField reloadInputViews] after setting the autocorrection type.

Be aware that this resets the state of the keyboard. If you were viewing symbols (instead of letters), after calling reloadInputViews the keyboard will be reset to letter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号