How to toggle autocorrectionType on and off for an existing UITextView

Viewed 8226

I have a UITextView in my iPhone app for which I want to be able to toggle the autocorrectionType.

When a user is editing the text view, I want the autocorrectionType to be set to UIAutocorrectionTypeYes. When the text view is not being edited, I want the autocorrectionType to be set to UIAutocorrectionTypeNo (because I don't want any of the red-dotted underlines generated by autocorrection to be visible)

Simply toggling the autocorrectionType like this:

myTextView.autocorrectionType = UITextAutocorrectionTypeYes;
myTextView.autocorrectionType = UITextAutocorrectionTypeNo;

Doesn't seem to work. Are there limitations on when I can toggle the autocorrectionType or in which situations the changed autocorrectionType takes effect?

EDIT:

To clarify: There are no issues setting the autocorrectionType when initializing the UITextView. The problem arises when I want to change the autocorrectionType for an existing UITextView. In my case I want to give users the advantage of autocorrection when they edit a UITextView, but don't want any spelling errors pointed out with the red-dotted underlines when the UITextView is not being edited - in part because I am also exporting a UIView containing the UITextView as an image. The problem is that just changing the value of my UITextView's autocorrectionType property doesn't work.

5 Answers
func activateTextViewAutocorrection() {

    textView.autocorrectionType = .default
    textView.reloadInputViews()
}

func deactivateTextViewAutocorrection() {

    textView.autocorrectionType = .no
    textView.reloadInputViews()
}
Related