iPhone Programming: Deactivate spell check in UITextView

后端 未结 4 1183
野趣味
野趣味 2020-12-17 20:48

UITextAutocorrectionTypeNo didn\'t work for me.

I\'m working on a crossword app for the iPhone. The questions are in UITextViews and I use UITextFields for the User-

相关标签:
4条回答
  • 2020-12-17 21:19

    I had the same problem. The solution is very simple but not documented: You can only change the properties defined in the UITextInputTraits protocol while the UITextView in question is NOT the first responder. The following lines fixed it for me:

    [self.textView resignFirstResponder];
    self.textView.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.textView becomeFirstResponder];
    

    Hope this helps somebody.

    0 讨论(0)
  • 2020-12-17 21:19

    One potentially-helpful tip following from Engin Kurutepe's answer:

    If you have subclassed UITextView, you can override the UITextInputTraits in the subclass implementation of becomeFirstResponder, something like this:

    -(BOOL)becomeFirstResponder {
        self.spellCheckingType = UITextSpellCheckingTypeNo;
        self.autocorrectionType = UITextAutocorrectionTypeNo;
        self.autocapitalizationType = UITextAutocapitalizationTypeNone;
        return [super becomeFirstResponder];
    }
    

    There is then no need to explicitly resign/becomeFirstResponder around your trait changes.

    0 讨论(0)
  • 2020-12-17 21:25

    Got a solution but it's not really how it should be. If someone knows something better, please tell me.

    The autocorrection executes, after the first touche. So I alloc a new UITextView and set him like the touched TextView. Then I replace the touched textView with my new TextView. So every UITextView instance could only be touched once and disappears. :)

    //UITextView delegate method in my Riddle-Class
    
    -(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
        ...CODE FROM FIRST POST HERE...
    
        // ADDED CODE:
        for (int i = 0; i < [textViews count]; i++) {
            if (textView == [textViews objectAtIndex:i]) {
                UITextView *trickyTextView = [[UITextView alloc] initWithFrame:textView.frame];
                trickyTextView.text = textView.text;
                trickyTextView.font = textView.font;
                trickyTextView.autocorrectionType = UITextAutocorrectionTypeNo;
                trickyTextView.textColor = textView.textColor;
                trickyTextView.backgroundColor = textView.backgroundColor;
                trickyTextView.delegate = self;
                trickyTextView.scrollEnabled = NO;
                [textViews replaceObjectAtIndex:i withObject:trickyTextView];
                [textView removeFromSuperview];
                [zoomView addSubview:trickyTextView];
                [trickyTextView release];
                break;
            }
        }
        return NO;
    }
    
    0 讨论(0)
  • 2020-12-17 21:35

    Important

    Disabling spell check will NOT update the UI of the red-line until the text itself is also updated. Simply setting the spellCheck to NO is not enough.

    To force a UI update, set the spellcheck property to NO, then toggle the text blank then back, like so:

    _textView.spellCheckingType = UITextSpellCheckingTypeNo;
    
    NSString *currentText = _textView.text;
    NSAttributedString *currentAttributedText = _textView.attributedText;
    _textView.text = @"";
    _textView.attributedText = [NSAttributedString new];
    _textView.text = currentText;
    if (currentAttributedText.length > 0) {
        _textView.attributedText = currentAttributedText;
    }
    
    0 讨论(0)
提交回复
热议问题