2

我正在做一个自定义键盘,但我想使用一些默认键,例如空格键和删除键。这是我用来禁用 key az 的代码。

- (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);}

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

键盘

4

2 回答 2

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;
    }
 }
于 2012-09-03T05:00:39.117 回答
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);
}

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

于 2012-09-03T05:29:43.613 回答