Ios 禁用UITextField键盘快捷键建议
是否有一种简单的方法可以从Ios 禁用UITextField键盘快捷键建议,ios,ios7,uitextfield,uitextview,Ios,Ios7,Uitextfield,Uitextview,是否有一种简单的方法可以从UITextField中删除键盘快捷键建议 可以使用以下命令删除键入更正:[textField setAutocorrectionType:UITextAutocorrectionTypeNo]但是,这对快捷方式没有影响 影响sharedMenuController也不会挤压它 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [UIMenuController sharedMenuCont
UITextField
中删除键盘快捷键建议
可以使用以下命令删除键入更正:[textField setAutocorrectionType:UITextAutocorrectionTypeNo]代码>但是,这对快捷方式没有影响
影响sharedMenuController也不会挤压它
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[UIMenuController sharedMenuController].menuVisible = NO;
return NO;
}
目标-C
textField.autocorrectionType = UITextAutocorrectionTypeNo;
Swift
textField.autocorrectionType = .no
文档仅使用此选项
textField.autocorrectionType = UITextAutocorrectionTypeNo;
通过实现UITextFieldDelegate
方法并手动设置UITextField的text属性,解决了这个问题
默认情况下,在模拟器中,您可以通过键入“omw”来测试这种行为,它应该建议“在我的路上!”。下面的代码将阻止此操作注意:这也会禁用自动更正和检查拼写,这在我的情况下是正常的。
- (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;
}
上述答案可能不适用于剪切/复制/粘贴情况。例如,在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]];
}
使用自动更正类型:
[mailTextField setAutocorrectionType:UITextAutocorrectionTypeNo] Swift 3.x或更高版本:
textField.autocorrectionType = .no
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
}
textField.autocorrectionType = .no
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
}