如何启用关键'空间'&禁用密钥a-z时,在iPhone键盘上删除密钥

时间:2012-09-03 04:39:25

标签: iphone ios uitextfield uikeyboard

我正在做一个自定义键盘,但我想使用一些默认键,如空格和删除键。这是我用来禁用密钥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);}

这是屏幕截图:(禁用其他键后我无法删除))

keyboard

2 个答案:

答案 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);
}

此代码现已更新为仅允许使用空格,输入和删除。