26

Is there a simple way to remove keyboard shortcut suggestions from a UITextField?

It is possible to remove typing correction with: [textField setAutocorrectionType:UITextAutocorrectionTypeNo]; however this as no effect on shortcuts.

Affecting the sharedMenuController also does not squash this.

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

enter image description here

Matt
  • 2,329
  • 1
  • 21
  • 23

9 Answers9

46

Objective-C

textField.autocorrectionType = UITextAutocorrectionTypeNo;

Swift

textField.autocorrectionType = .no

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

Lepidopteron
  • 6,056
  • 5
  • 41
  • 53
4

Use this only

  textField.autocorrectionType = UITextAutocorrectionTypeNo;
Parvendra Singh
  • 965
  • 7
  • 19
4

Solved this by implementing a UITextFieldDelegate method and manually setting the text property of UITextField.

By default in the simulator you can test this behaviour by typing "omw" which should suggest "On my way!". The following code will block this. Note: this does also disable auto-correction and check spelling which was ok in my case.

- (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;
}
Matt
  • 2,329
  • 1
  • 21
  • 23
1

Matt's solution converted to Swift 5:

func textField(_ textField: UITextField,
               shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool {
    // Pass through backspace and character input
    if (range.length > 0 && string.isEmpty) {
        textField.text?.removeLast()
    } else {
        textField.text?.append(string)
    }
    // Return false to override default UITextField behaviors
    return false
}
Lauri Nurmi
  • 566
  • 1
  • 3
  • 14
0
UITextField* f = [[UITextField alloc] init];
f.autocorrectionType = UITextAutocorrectionTypeNo;
Clad Clad
  • 2,653
  • 1
  • 20
  • 32
0

The above mentioned answer may not work with cut / copy / paste situation. For example on cutting and pasting text in UITextField, the result is not as per default functionality.

Below is a similar approach:

-(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]];
}
AJPerez
  • 3,435
  • 10
  • 61
  • 91
Anmol Suneja
  • 97
  • 11
0
textField.autocorrectionType = .No
GregP
  • 1,584
  • 17
  • 16
0

Use AutocorrectionType :

[mailTextField setAutocorrectionType:UITextAutocorrectionTypeNo];

0

Swift 3.x or above:

textField.autocorrectionType = .no
Hemang
  • 26,840
  • 19
  • 119
  • 186