2014-04-03 118 views
12

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

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

影响sharedMenuController也不会挤压这个。

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

enter image description here

回答

2

通过实施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; 
} 
0
UITextField* f = [[UITextField alloc] init]; 
f.autocorrectionType = UITextAutocorrectionTypeNo; 
3

使用此仅

textField.autocorrectionType = UITextAutocorrectionTypeNo; 
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]]; 
} 
1
textField.autocorrectionType = .No 
0

使用AutocorrectionType:

[mailTextField setAutocorrectionType:UITextAutocorrectionTypeNo];

1

夫特3.x或以上:

textField.autocorrectionType = .no