How to toggle autocorrectionType on and off for an existing UITextView

﹥>﹥吖頭↗ 提交于 2019-11-29 06:11:12

问题


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.


回答1:


Here's an easy way to do this for the image export scenario :

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    // Turn spell check on
    textView.autocorrectionType = UITextAutocorrectionTypeYes;
    return YES;
}


- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    // Turn spell check off and clean up red squiggles.
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    NSString *currentText = textView.text;
    textView.text = @"";
    textView.text = currentText;
    return YES;
}



回答2:


You can try to first hide the keyboard first and then displaying it again. Also update the uitextview. If [UITextView setNeedsDisplay] doesn't work for you, try [UITextView insertText:] and then [UITextView deleteBackward]

[textView resignFirstResponde];
textView.autocorrectionType = UITextAutocorrectionTypeNo;
[textView becomeFirstResponder];
[textView setNeedsDisplay];

or

[textView insertText:@" "];
[textView deleteBackward];



回答3:


In addition to changing the autocorrection type to UITextAutoCorrectionNo, the UITextView must be forced to reevaluate its correction state. setNeedsRedraw is insufficient but setting the text to itself, e.g.

textView.autocorrectionType = UITextAutocorrectionTypeNo;
textView.text = textView.text;

makes the red dashed lines go away. NOTE: this workaround relies on undocumented behavior and is not guaranteed to work on future iOS releases.




回答4:


Try calling -setNeedsDisplay on the text view after you've changed the autocorrectionType. This will force the text view to redraw and will hopefully clear the red underlines.

myTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[myTextView setNeedsDisplay];


来源:https://stackoverflow.com/questions/6867719/how-to-toggle-autocorrectiontype-on-and-off-for-an-existing-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!