How to add characters to a native iOS keyboard type?

Asked

Viewed 407 times

0

Scenario: On an app screen, the user must inform a data type that is only numbers to perform a search. To avoid using text, I set up a keyboard for the type: Number Pad. But I need other characters to appear next to the numbers, in this case the hyphen ( - ) and the endpoint ( . ).

Does anyone have any idea how I can do this?.

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

Below is a picture of how it is and how I intend it to look:

Estado Atual

Como preciso que fique

But if anyone can guide me I’d be grateful!

Thank you

1 answer

2

There is an option on UITextField with keyboard for numbers and scores, but it is the whole keyboard, just change the initial view as soon as it is displayed.

Anyway, you can use a validation in the method available below 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 my example is the inputTest, to accept only numbers, dots and hyphens.

See if it suits you.

  • I’ll put an image to illustrate better the idea of how it is and how would be the keyboard I want

  • I get it. But if you really need a keyboard with only the possible characters, it’s only possible to include a view off the keyboard, like what we see in browsers for example. Or by creating your own keyboard, either through the new iOS 8 feature or on inputView country.

  • Puts to lascado rs I’ll see if there are any projects for this on the net

Browser other questions tagged

You are not signed in. Login or sign up in order to post.