2010-07-23 18 views
8

UITextAutocorrectionTypeNo對我無效。iPhone編程:取消UITextView中的拼寫檢查

我正在爲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。

回答

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

我有同樣的問題。該解決方案非常簡單,但沒有記錄:您只能更改UITextInputTraits協議中定義的屬性,而UITextView不是第一響應者。以下幾行爲我解決了這個問題:

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

希望這有助於某人。

8

一個潛在的,有用的提示,從Engin Kurutepe的回答如下:

如果你的子類UITextView的,你可以在子類實現的becomeFirstResponder覆蓋UITextInputTraits,像這樣:

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

有那麼你就不需要明確地圍繞你的特質變化來進行resign/becomeFirstResponder