【问题标题】:iPhone Programming: Deactivate spell check in UITextViewiPhone 编程:停用 UITextView 中的拼写检查
【发布时间】:2011-03-20 03:07:06
【问题描述】:

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。

【问题讨论】:

    标签: objective-c uitextview spell-checking


    【解决方案1】:

    我遇到了同样的问题。解决方案非常简单,但没有记录:您只能更改UITextInputTraits 协议中定义的属性,而有问题的UITextView 不是第一响应者。以下几行为我修复了它:

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

    希望这对某人有所帮助。

    【讨论】:

    【解决方案2】:

    来自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,请将拼写检查属性设置为 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;
      }
      

      【讨论】:

        【解决方案4】:

        找到了解决方案,但实际上并非如此。 如果有人知道更好的事情,请告诉我。

        在第一次触摸后执行自动更正。 所以我分配了一个新的 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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-02
          相关资源
          最近更新 更多