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;
}
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;
}
Objective-C
textField.autocorrectionType = UITextAutocorrectionTypeNo;
Swift
textField.autocorrectionType = .no
Utilisez ceci seulement
textField.autocorrectionType = UITextAutocorrectionTypeNo;
Utilisez AutocorrectionType:
[mailTextField setAutocorrectionType: UITextAutocorrectionTypeNo];
Swift 3.x ou supérieur:
textField.autocorrectionType = .no
UITextField* f = [[UITextField alloc] init];
f.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocorrectionType = .No
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]];
}