iPhone编程:如何在UITextView中关闭拼写检查

9

UITextAutocorrectionTypeNo对我没有起作用。

我正在为iPhone开发一个填字游戏应用程序。问题是在UITextViews中,我使用UITextFields为每个字母的用户输入。通过触摸问题(UITextView),第一个答案字符的TextField变为FirstResponder。

一切都很好,但UITextViews仍然进行拼写检查,并标记问题中的错误单词,即使我将它们设置为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;
}

我没有在其他任何地方调用UITextView。

4个回答

22

我遇到了同样的问题。解决方法非常简单,但没有文档记录:只能在问题的UITextView不是第一响应者时更改在UITextInputTraits协议中定义的属性。以下几行代码为我解决了这个问题:

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

希望这能对某人有所帮助。


请注意,如果禁用了拼写检查(红色下划线),您需要强制进行UI更新以消除红色波浪线。请参见此处:https://dev59.com/Wk_Sa4cB1Zd3GeqP_EhC#51958597 - Albert Renshaw

8

以下是从Engin Kurutepe的回答中可能有用的提示:

如果您已经对UITextView进行了子类化,您可以在becomeFirstResponder的子类实现中覆盖UITextInputTraits,例如:

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

因此,在更改特质时,无需显式地 resign/becomeFirstResponder


3

重要

禁用拼写检查不会更新红线的UI,直到文本本身也被更新。仅仅将spellCheck设置为NO是不够的。

要强制更新UI,请将spellcheck属性设置为NO,然后切换文本为空,然后再切换回来,就像这样:

_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

已经有一个解决方案,但并不是最好的方法。 如果有更好的方法,请告诉我。

自动更正在第一次触摸后执行。 因此,我分配了一个新的UITextView,并将其设置为所触摸的TextView。 然后,我用我的新TextView替换了所触摸的textView。 因此,每个UITextView实例只能被触摸一次并消失。 :)

//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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接