禁用UITextField键盘快捷方式建议

时间:2014-04-03 13:38:15

标签: ios ios7 uitextfield uitextview

是否有一种简单的方法可以从UITextField删除键盘快捷方式建议

可以使用[textField setAutocorrectionType:UITextAutocorrectionTypeNo];删除输入更正,但这对快捷方式没有影响。

影响sharedMenuController也不会压缩它。

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

enter image description here

8 个答案:

答案 0 :(得分:28)

<强>目标C

textField.autocorrectionType = UITextAutocorrectionTypeNo;

<强>夫特

textField.autocorrectionType = .no

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

答案 1 :(得分:3)

仅限此

  textField.autocorrectionType = UITextAutocorrectionTypeNo;

答案 2 :(得分:2)

通过实现UITextFieldDelegate方法并手动设置UITextField的text属性解决了这个问题。

默认情况下,您可以在模拟器中输入&#34; omw&#34; 来测试此行为,这应该建议&#34;在我的路上!&#34; 。以下代码将阻止此操作。 注意:这也会禁用自动更正检查拼写,这在我的情况下是正常的。

- (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;
}

答案 3 :(得分:0)

UITextField* f = [[UITextField alloc] init];
f.autocorrectionType = UITextAutocorrectionTypeNo;

答案 4 :(得分:0)

上述答案可能不适用于剪切/复制/粘贴情况。例如,在UITextField中剪切和粘贴文本时,结果不是默认功能。

以下是类似的方法:

-(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]];
}

答案 5 :(得分:0)

使用AutocorrectionType:

[mailTextField setAutocorrectionType:UITextAutocorrectionTypeNo];

答案 6 :(得分:0)

Swift 3.x或以上:

textField.autocorrectionType = .no

答案 7 :(得分:0)

Matt 的解决方案转换为 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
}