我正在做一个自定义键盘,但我想使用一些默认键,如空格和删除键。这是我用来禁用密钥a-z的代码。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSMutableString * character;
NSCharacterSet *disableLetters = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"] invertedSet];
NSRange location = [character rangeOfCharacterFromSet:disableLetters];
return (location.location == NSNotFound);}
这是屏幕截图:(禁用其他键后我无法删除))
答案 0 :(得分:0)
试试这个:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//NSMutableString * character;
NSCharacterSet *disableLetters = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"] invertedSet];
NSRange location = [string rangeOfCharacterFromSet:disableLetters];
if(location.location != NSNotFound);
{
return YES;
}
else
{
return NO;
}
}
答案 1 :(得分:0)
我修复了您的代码存在的一些问题,因此现在可以使用:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *disabledLetters = [[NSCharacterSet characterSetWithCharactersInString:@" "] invertedSet];
NSRange location = [string rangeOfCharacterFromSet:disabledLetters];
return (location.location == NSNotFound);
}
此代码现已更新为仅允许使用空格,输入和删除。