Search Posts in my Blog

Monday 19 March 2012

NumberPad With Decimal Point


Of the various keyboards you can choose when developing iPhone apps, the number pad doesn’t come with a decimal point. There is a blank button in the bottom left corner that doesn’t do anything, so I’m going to show you how to put a decimal point button there to look like this:

Screen shot 2010-03-13 at 17.40.13 Screen shot 2010-03-13 at 17.40.19
There are a few other tutorial around that show you how to do this, but i believe mine is better….because the code is simpler to use, its more flexible, and the UI colors and button states are perfectly matched to the rest of the keyboard (unlike some of the other tutorials) The code you use to implement this will look like this:
01@interface DecimalPointNumberPadViewController : UIViewController <UITextFieldDelegate> {
02    NumberKeypadDecimalPoint *numberKeyPad;
03}
04@end
05 
06@implementation DecimalPointNumberPadViewController
07- (void) textFieldDidBeginEditing:(UITextField *)textField {
08    numberKeyPad = [[NumberKeypadDecimalPoint keypadForTextField:textField] retain];
09}
10- (void)textFieldDidEndEditing:(UITextField *)textField {
11    [numberKeyPad release];
12}
13@end
  • it works on any number of UITextFields that are displayed on your view controller.
  • It will only add one decimal point per text field.
To achieve this, here are the basic steps of what i do:
  1. Create a custom UIButton with clear background and dark grey text.
  2. For the highlighted state i change the background image of the button and the text color to be white
  3. I find the UIKeyboard in the application window and add the custom button at the required location
  4. I add a delegate to the button to listen for click events and pass the event to a handler which adds a decimal point to the current UITextField
Download Code from HERE

No comments:

Post a Comment