How do I add characters to a native iOS keyboard type?

0

Scenario: In an app screen, the user should enter a data type that is only a number to perform a search. To avoid using text, I set up a keyboard for the type: Number Pad. However, I need to display other characters together, in this case the hyphen (-) and the end-point (.).

Does anyone have any idea how I can do this?

I had the idea of at least instantiating a class of UITextFiled and editing the type of keyboard that appears for it.

Below is an image of how it is and how I want it to be:

But if anyone can guide me, I'll be grateful!

Thank you

    
asked by anonymous 13.10.2014 / 20:00

1 answer

2

There is an option in the UITextField with keyboard for numbers and punctuations, but it is the entire keyboard, it only changes the initial view as it is displayed.

Anyway, you can use a validation in the method below available delegate UITextFieldDelegate . Do something like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.inputTest) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789.-"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

        return [string isEqualToString:filtered];
    }

    return YES;
}

So you will restrict the field, which in this example is the inputTest , to accept only numbers, points, and hyphens.

See if it caters to you.

    
13.10.2014 / 20:28