触摸空格键时如何防止键盘从数字更改为字母? [英] How to prevent keyboard changing from Numbers to Alphabet when the space key is touched?
问题描述
我在表格上输入 UITextFields
来输入值。
其中一些字段只接受数字。我使用 UIKeyboardTypeNumbersAndPunctuation
作为keyboardType,使用 shouldChangeCharactersInRange
来过滤字符。
I have UITextFields
on a table to enter values.
Some of these fields accept only numbers. I am using UIKeyboardTypeNumbersAndPunctuation
for the keyboardType, and shouldChangeCharactersInRange
to filter the characters.
此外,所有更正都被禁用:
Also, all the corrections are disabled:
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
在仅数字字段上,触摸空格键时,键盘将更改为字母。
我知道这是默认行为。
我想忽略空格键,不想更改键盘类型。
On the numeric only fields, when the space key is touched, the keyboard changes to Alphabet. I know that this is the default behavior. I want to ignore the space key, and don't want the keyboard type change.
有没有办法改变这种默认行为?
Is there some way to change this default behavior?
PS:其他数字键盘类型不是一个选项。我需要标点符号!
PS: the other numeric keyboard types are not an option. I need the Punctuation!
谢谢
推荐答案
我不认为可以修改键盘行为。
I do not think it is possible to modify the keyboard behavior.
但是,你可以实现 textField:shouldChangeCharactersInRange:replacementString:来自UITextFieldDelegate协议拦截空间(和撇号),如下所示它似乎工作:
However, you could implement the textField:shouldChangeCharactersInRange:replacementString: from the UITextFieldDelegate protocol to intercept the space (and apostrophe) like this and it seems to work:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@" "] || [string isEqualToString:@"'"]) {
NSMutableString *updatedString = [NSMutableString stringWithString:textField.text];
[updatedString insertString:string atIndex:range.location];
textField.text = updatedString;
return NO;
} else {
return YES;
}
}
这篇关于触摸空格键时如何防止键盘从数字更改为字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!