web-dev-qa-db-fra.com

Désactiver les suggestions de raccourci clavier UITextField

Existe-t-il un moyen simple de supprimer les suggestions de raccourcis clavier d’une UITextField?

Il est possible de supprimer la correction de frappe avec: [textField setAutocorrectionType:UITextAutocorrectionTypeNo];, mais cela n’a aucun effet sur les raccourcis.

Affecter le sharedMenuController n'écrase pas non plus cela.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    [UIMenuController sharedMenuController].menuVisible = NO;
    return  NO;
}

enter image description here

16
Matt

Cela a été résolu en implémentant une méthode UITextFieldDelegate et en définissant manuellement la propriété text de UITextField.

Par défaut, dans le simulateur, vous pouvez tester ce comportement en tapant "omw" , ce qui devrait suggérer "On my way!" . Le code suivant bloquera ceci. Remarque: ceci désactive également correction automatique et vérifie l'orthographe , ce qui était correct dans mon cas.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // Pass through backspace and character input
    if (range.length > 0 && [string isEqualToString:@""]) {
        textField.text = [textField.text substringToIndex:textField.text.length-1];
    } else {
        textField.text = [textField.text stringByAppendingString:string];
    }
    // Return NO to override default UITextField behaviors
    return NO;
}
2
Matt

Objective-C

textField.autocorrectionType = UITextAutocorrectionTypeNo;

Swift

textField.autocorrectionType = .no

Documentation https://developer.Apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html

27
Lepidopteron

Utilisez ceci seulement 

  textField.autocorrectionType = UITextAutocorrectionTypeNo;
3
Parvendra Singh

Utilisez AutocorrectionType:

[mailTextField setAutocorrectionType: UITextAutocorrectionTypeNo];

0
MARK IOS Developer

Swift 3.x ou supérieur:

textField.autocorrectionType = .no
0
Hemang
UITextField* f = [[UITextField alloc] init];
f.autocorrectionType = UITextAutocorrectionTypeNo;
0
Clad Clad
textField.autocorrectionType = .No
0
GregP

La réponse mentionnée ci-dessus peut ne pas fonctionner avec une situation couper/copier/coller. Par exemple, lors du copier-coller de texte dans UITextField, le résultat n'est pas conforme à la fonctionnalité par défaut.

Ci-dessous une approche similaire:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

NSString *textFieldNewText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if ([string isEqualToString:@""]) {
        // return when something is being cut
        return YES;
    }
    else
    {
        //set text field text
        textField.text=textFieldNewText;
        range.location=range.location+[string length];
        range.length=0;
        [self selectTextInTextField:textField range:range];
    }
    return NO;
}


//for handling cursor position when setting textfield text through code
- (void)selectTextInTextField:(UITextField *)textField range:(NSRange)range {

    UITextPosition *from = [textField positionFromPosition:[textField beginningOfDocument] offset:range.location];
    UITextPosition *to = [textField positionFromPosition:from offset:range.length];
    [textField setSelectedTextRange:[textField textRangeFromPosition:from toPosition:to]];
}
0
Anmol Suneja