2010-07-23 8 views
8

UITextAutocorrectionTypeNo no funcionó para mí.Programación de iPhone: Desactivar el corrector ortográfico en UITextView

Estoy trabajando en una aplicación de crucigramas para el iPhone. Las preguntas están en UITextViews y utilizo UITextFields para la entrada de usuario de cada letra. Al tocar una pregunta (UITextView), TextField para la primera respuesta char se convierte en FirstResponder.

Todo funciona bien, pero las UITextViews siguen revisando la ortografía y marcan las palabras incorrectas en la pregunta, incluso si las configuro 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; 
} 

No llamo UITextView en cualquier otro lugar.

Respuesta

0

Tiene una solución, pero no es como debería ser. Si alguien sabe algo mejor, por favor dígame.

La autocorrección se ejecuta, después del primer toque. Así que asigno una nueva UITextView y lo configuro como el TextView tocado. Luego reemplazo el textView tocado con mi nuevo TextView. De modo que cada instancia de UITextView solo podría tocarse una vez y desaparecería. :)

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

Tuve el mismo problema. La solución es muy simple pero no está documentada: solo puede cambiar las propiedades definidas en el protocolo UITextInputTraits, mientras que el UITextView en cuestión NO es el primero en responder. Las siguientes líneas lo arreglaron para mí:

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

Espero que esto ayude a alguien.

8

Uno potencialmente útiles punta siguiente desde Engin Kurutepe 's respuesta:

Si ha subclase UITextView, puede anular la UITextInputTraits en la implementación subclase de becomeFirstResponder, algo como esto:

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

Hay entonces no es necesario explícitamente resign/becomeFirstResponder alrededor de sus cambios de rasgo.

Cuestiones relacionadas