How to toggle autocorrectionType on and off for an existing UITextView

前端 未结 4 823
一个人的身影
一个人的身影 2020-12-30 02:42

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 autocorrectionTy

相关标签:
4条回答
  • 2020-12-30 03:00

    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;
    }
    
    0 讨论(0)
  • 2020-12-30 03:11

    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.

    0 讨论(0)
  • 2020-12-30 03:14

    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];
    
    0 讨论(0)
  • 2020-12-30 03:19

    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];
    
    0 讨论(0)
提交回复
热议问题