Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Programming: Deactivate spell check in UITextView

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-Input of each letter. By touching a question(UITextView), the TextField for the first answer char becomesFirstResponder.

It all works fine but the UITextViews are still spell checking and mark the wrong words in the question, even if I set them UITextAutocorrectionTypeNo .

//init of my Riddle-Class

...

for (int i = 0; i < theQuestionSet.questionCount; i++) {

    Question *myQuestion = [theQuestionSet.questionArray objectAtIndex:i];
    int fieldPosition = theQuestionSet.xSize * myQuestion.fragePos.y + myQuestion.fragePos.x;
 CrosswordTextField *myQuestionCell = [crosswordCells objectAtIndex:fieldPosition];
 questionFontSize = 6;
 CGRect textViewRect = myQuestionCell.frame;

 UITextView *newView = [[UITextView alloc] initWithFrame: textViewRect];
 newView.text = myQuestion.frageKurzerText;
 newView.backgroundColor = [UIColor colorWithRed: 0.5 green: 0.5 blue: 0.5 alpha: 0.0 ];
 newView.scrollEnabled = NO;
 newView.userInteractionEnabled = YES;
 [newView setDelegate:self];
 newView.textAlignment = UITextAlignmentLeft;
 newView.textColor = [UIColor whiteColor];
 newView.font = [UIFont systemFontOfSize:questionFontSize];
 newView.autocorrectionType = UITextAutocorrectionTypeNo;
 [textViews addObject:newView];
 [zoomView addSubview:newView];
 [newView release];
}

...

//UITextView delegate methode in my Riddle-Class

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {

     textView.autocorrectionType = UITextAutocorrectionTypeNo;  

     for (int i = 0; i < [questionSet.questionArray count]; i++) {
      if ([[[questionSet.questionArray objectAtIndex:i] frageKurzerText] isEqualToString:textView.text]) {
        CrosswordTextField *tField = [self textfieldForPosition:
            [[questionSet.questionArray objectAtIndex:i] antwortPos]]; 
        markIsWagrecht = [[questionSet.questionArray objectAtIndex:i] wagrecht];
        if ([tField isFirstResponder]) [tField resignFirstResponder];
             [tField becomeFirstResponder];
        break;
      }
 }
 return NO;
}

I don't call UITextView on any other place.

like image 380
Thorsten Avatar asked Jul 23 '10 14:07

Thorsten


3 Answers

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;
}
like image 107
Albert Renshaw Avatar answered Oct 23 '22 18:10

Albert Renshaw


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.

like image 23
Engin Kurutepe Avatar answered Oct 23 '22 18:10

Engin Kurutepe


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.

like image 45
jdc Avatar answered Oct 23 '22 17:10

jdc