开发者

UITextField - UIKeyboardTypeDecimalPad on iPad?

On the iPad...

textField.keyboardType = UIKeyboardTypeDecimalPad;

...just shows the regular keyboard, but starting on the numbers at the top (with lots of punctuation und开发者_C百科erneath).

But it's to type in a number. I only want the users to be able to type numbers and a decimal point, like UIKeyboardTypeDecimalPad does on the iPhone.

Is there any way to make the irrelevant keys go away and leave my user alone?


Setting a UITextField's keyboardType only makes it easier for a user to enter appropriate characters. Even on the iPhone, users can enter other characters via a hardware keyboard, or by pasting in a string.

Instead, implement UITextFieldDelegate's -textField:shouldChangeCharactersInRange:replacementString: to validate user input. You also probably don't really want to hardcode the digits 0-9 and a period. Users in some locales, for example, separate whole numbers from decimals with a comma:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *candidate = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    if (!candidate || [candidate length] < 1 || [candidate isEqualToString:@""])
    {
        return YES;
    }
    NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:candidate];
    if (!number || [number isEqualToNumber:[NSDecimalNumber notANumber]])
    {
        return NO;
    }
    return YES;
}

Alternately, you might perform validation when the user finishes entering text, in –textFieldShouldReturn:, – textFieldShouldEndEditing:, or – textFieldDidEndEditing: as desired.


This is not necessarily a foolproof solution, but it matched my needs and might be helpful to others. The concern would be that the number chars are hardcoded. I'm not sure of locales that use other numbers chars, but they can be added as needed.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *candidate = [[textField text] stringByReplacingCharactersInRange:range withString:string];

    // Ensure that the local decimal seperator is used max 1 time
    NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
    NSString *decimalSymbol = [formatter decimalSeparator];
    if ([candidate componentsSeparatedByString:decimalSymbol].count > 2) return NO;

    // Ensure that all the characters used are number characters or decimal seperator
    NSString *validChars = [NSString stringWithFormat:@"0123456789%@", decimalSymbol];
    if ([candidate stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:validChars]].length) return NO;

    return YES;
}


I know this question has been inactive for quite a while, but this might help somebody out there.

You can try implementing this one https://github.com/azu/NumericKeypad. The coder implemented his own number pad buy subclassing the UITextField


Link this up to your textField's editingChanged method. You can edit the characters around as you feel the need to.

- (IBAction)numberValidator:(id)sender {
    NSMutableString *txt1 = [[NSMutableString alloc] initWithString:_numbersOnlyField.text];
    for (unsigned int i = 0; i < [txt1 length]; i++) {
        NSString *character = [[NSString alloc] initWithFormat:@"%C", [txt1 characterAtIndex:i]];

        // Erase any characters you don't like, but you don't feel the user needs to be alerted about
        else if ([character isEqualToString:@" "]){
            [txt1 deleteCharactersInRange:NSMakeRange(i, 1)];
            _numbersOnlyField.text = [[NSString alloc] initWithString:txt1];
        }

        // Alert the user that they shouldn't enter anything besides numbers
        if ([character integerValue] == 0 && ![character isEqualToString:@"0"]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Numbers only please!"
                                                           message: @"Please only enter numbers in the \"<your text field's name here>\" text field"
                                                          delegate: nil
                                                 cancelButtonTitle:@"Sorry..."
                                                 otherButtonTitles:nil];
            [alert show];
            [txt1 deleteCharactersInRange:NSMakeRange(i, 1)];
            _numbersOnlyField.text = [[NSString alloc] initWithString:txt1];
        }
    }
}

You can also do it programmatically through the UIControlEventValueChanged method.

This is just a solution I came up with myself and I definitely don't consider myself a professional, so there might be some flaws I'm overlooking, but this works pretty well for me.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜