开发者

iPhone Keyboard Filter Question

开发者 https://www.devze.com 2022-12-08 03:21 出处:网络
I want to limit the character users can type in iPhone\'s keyboard, so I created an array of my own. e.g. The array including 0~9 and a dot to enable users to type a price. Then I can return NO for -(

I want to limit the character users can type in iPhone's keyboard, so I created an array of my own. e.g. The array including 0~9 and a dot to enable users to type a price. Then I can return NO for -(BOOL)textField:shouldChangeCharactersInRange:replacementString: if the replace string is not in the array.

The problem is that the backspace button is also disabled when I use this array to filter text. Any ideas about how to enable backspace button?

Another problem is that I want to let users type their names and therefore I don't want to let them switch to numbers and punctuaction (backspace button is also locked if I use an array to fi开发者_JAVA技巧lter). How to disable the switch button on the keyboard (Now I just limit them to type a~z, blank and "." , but I think disable the switch button might be a better way)?


I find a way (maybe not good enough, but it can do the work for backspace function):

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == txtChargeAmt)
    {
        if(string.length == 0)  //backspace button is pressed
        {
            textField.text = [textField.text substringToIndex:(textField.text.length - 1)];
            return NO;
        }

        for(NSString *s in arrNumberAndDot)
        {
            if([string isEqualToString:s])
            {
                return YES;
            }
        }
        return NO;
    }
    else
        return YES;
}

Other ideas about the backspace issue are welcomed. And how to disable the switch button then?


I guess I am not sure why you would want to use the same TextField for these two different types of input.

I would have two fields, an alphanumeric field for name entry and a numeric field for number entry.

Or am I not getting your question?


This will do what you want a little more succinctly (and efficiently):

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    NSCharacterSet *validCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];

    BOOL shouldChange = 
        [string length] == 0 || // deletion
        textField != txtChargeAmt || // not the field we care about
        [string rangeOfCharacterFromSet:validCharacterSet].location != NSNotFound;

    if (!shouldChange)
    {
        // Tell the user they did something wrong.  There's no NSBeep() 
        // on the iPhone :(
    }

    return shouldChange;
}

I'd construct that character set somewhere else so you you'd only have to do it once, but you get the idea. Anyone have any thoughts on what to do to alert the user they used an invalid character? I'm trying to solve a similar problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消