UITextField - UIKeyboardTypeDecimalPad on iPad?

On the iPad ...

textField.keyboardType = UIKeyboardTypeDecimalPad; 

... just shows a regular keyboard, but starting with the numbers at the top (with a lot of punctuation below).

But this is to enter a number. I just want users to be able to enter numbers and decimal point, for example UIKeyboardTypeDecimalPad on iPhone.

Is there a way to make unnecessary keys go away and leave my user alone?

+7
source share
4 answers

Customizing the UITextField keyboardType only makes it easier for the user to enter the appropriate characters. Even on iPhone, users can enter other characters using the hardware keyboard or paste into a string.

Instead, execute UITextFieldDelegate -textField:shouldChangeCharactersInRange:replacementString: to verify user input. You probably also don't want to hardcode the numbers 0-9 and period. Users in some locales, for example, extract integers from decimal places of 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; } 

Alternatively, you can check when the user finishes typing in –textFieldShouldReturn: – textFieldShouldEndEditing: or – textFieldDidEndEditing: as desired.

+8
source

This is not necessarily a reliable solution, but it meets my needs and may be useful to others. The concern would be that numeric characters are hardcoded. I'm not sure about locales that use characters of other numbers, 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; } 
+2
source

I know this question has been inactive for a long time, but it might help someone out there.

You can try to implement this https://github.com/azu/NumericKeypad . The encoder has implemented its own license plate buy subclassing UITextField

+1
source

Associate this with your textField editingChanged method. You can edit the characters as you feel the need.

 - (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 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 this programmatically using the UIControlEventValueChanged method.

This is just a solution that I came up with myself, and I definitely do not consider myself a professional, so there may be some disadvantages that I miss, but it is very good for me.

0
source

All Articles