2010-07-23 4 views
8

UITextAutocorrectionTypeNo n'a pas fonctionné pour moi. Je travaille sur une application de mots croisés pour l'iPhone. Les questions sont dans UITextViews et j'utilise UITextFields pour l'entrée utilisateur de chaque lettre. En touchant une question (UITextView), TextField pour la première réponse char devient FirstResponder.Programmation iPhone: désactiver la vérification orthographique dans UITextView

Tout fonctionne très bien mais les UITextViews vérifient toujours l'orthographe et marquent les mauvais mots dans la question, même si je leur ai attribué 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; 
} 

Je n'appelle pas UITextView à un autre endroit.

Répondre

0

Vous avez une solution mais ce n'est pas vraiment ce qu'elle devrait être. Si quelqu'un sait quelque chose de mieux, s'il vous plaît dites-moi.

L'autocorrection s'exécute après la première touche. J'ai donc alloué une nouvelle UITextView et l'ai défini comme le TextView touché. Ensuite, je remplace le textView touché avec mon nouveau TextView. Ainsi, chaque occurrence de UITextView ne pouvait être touchée qu'une seule fois et disparaissait. :)

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

J'ai eu le même problème. La solution est très simple mais non documentée: Vous ne pouvez modifier que les propriétés définies dans le protocole UITextInputTraits alors que le UITextView en question n'est PAS le premier répondeur. Les lignes suivantes l'ont corrigé pour moi:

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

Espérons que cela aide quelqu'un.

8

Une astuce potentiellement utile suite de la réponse de Engin Kurutepe:

Si vous avez sous-classé UITextView, vous pouvez remplacer les UITextInputTraits dans la mise en œuvre de la sous-classe de becomeFirstResponder, quelque chose comme ceci:

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

Il est alors pas besoin de explicitement resign/becomeFirstResponder autour de vos changements de caractère.

Questions connexes