UPDATE: iOS 4.1 Now has this behaviour built into the SDK. You can set your keyboard type to be UIKeyboardTypeDecimalPad ! Finally !
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:

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:
@interface DecimalPointNumberPadViewController : UIViewController <UITextFieldDelegate> {
NumberKeypadDecimalPoint *numberKeyPad;
}
@end
@implementation DecimalPointNumberPadViewController
- (void) textFieldDidBeginEditing:(UITextField *)textField {
numberKeyPad = [[NumberKeypadDecimalPoint keypadForTextField:textField] retain];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[numberKeyPad release];
}
@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:
- Create a custom UIButton with clear background and dark grey text.
- For the highlighted state i change the background image of the button and the text color to be white
- I find the UIKeyboard in the application window and add the custom button at the required location
- 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
Update 5th June 2010: OS4 compatible and using only public API
As using private API’s is against the SDK agreement, I’ve updated the code to remove references to any private API. It is also now compatible with OS 4.0.
Update 20th July 2010: This code was part of the app I’m working on and was accepted by apple.
A copy of my XCode project is here:
Thanks
DecimalPointNumberPad-v4
|
Bob1601 |
Hi,
I’ve been working with this code and it seems to be working as in I’m not getting any errors but the decimal button just isn’t showing up. What am I missing? Has anyone else experienced something like this? I thought it might have something to do with the fact that I have 2 view controllers, but it still shouldnt really be causing that.
If anyone can help it would be great. I’m pretty confused at this stage.
Bob
Ivan |
I’m sure you are aware that you can receive notifications for when the keyboard appears. Why not use the UIKeyboardDidShowNotification instead of the timer to trigger the adding of the button? I guess it might be harder to animate it in in that case. The fact that the implementation uses a timer makes me uncomfortable though, but otherwise, great work.
Ralf |
Hi Bob1601,
the code should also work with a flip view. You should be adding your delegate in Interface Builder. Just right click on the “files owner” and connect your input fields.
Ralf
James |
Hi, i put your example in my code, but when the keypad dissapear, the button still in the view and is in nfornt of my tab bar, for hese reason my first tab button of my tab bar cant be use and when i clicked i activate the button. eny solution please?
croyboy |
Thanks
Works great
Ryan |
Wow..just what I’ve been looking for! You’re awesome.
Although I’m having troubles getting it to work with my project. I’m using it in a TabBarController based app. I’ve included the NumberKeypadDecimalPoint.h and .m files and copy pasted the rest of the code, but no go. Is there a trick to using this with a TabBar app?
Janice |
Good work on this, however, I have 3 views using a UITabBarController and this works only on the last view. The other two views don’t show the decimal point at all. Has anyone gotten this error?
Ryan |
Figured it out! Fingers crossed Apple accepts it.
Debabrata Biswas |
Very good tutorial.
MK |
Thanks!
Hasan Aydın |
Hello,
Can i change button text dynamically with ‘.’ or ‘,’ or ‘;’ or etc. ?
Thanks
Tarek Sakr |
Thanks a lot David for the code. I am sending you here to take your permission to add your code to our Sensible TableView product. We’d be really happy to give you a gift version in exchange. Please have a look at Sensible TableView at: http://www.sensiblecocoa.com . Thanks a lot.
admin |
Hi Tarek, sure you have my permission to use this code in your product.
kyu |
Thank you for great tutorial!!
Pingback: iPhone – Add the Return Key for UIKeyboardTypeNumberPad in iPhone SDK 4 « Eureka!
admin |
UPDATE: iOS 4.1 Now has this behaviour built into the SDK. You can set your keyboard type to be UIKeyboardTypeDecimalPad ! Finally !
Mawa |
Hi,
I got the SDK 4.1 and I cannot set the keyboard type to be UIKeyboardTypeDecimalPad, at least I do not see the option in Interface Builder.
Where in code can I add this?
thank you.
Pingback: How to add a decimal point or a done button to the iphone number pad | Software Promotion + SEO
pierre |
hi,
i’m a new developer on iphone.
i tried to add your code to my project, but it doesn’t work: i added “NumberKeypadDecimalPoint.h”, “NumberKeypadDecimalPoint.m” and the image to the project (the are in the project folder).
in the myproject.h i added:
- #import “NumberKeypadDecimalPoint.h” at the beginning of the file (after the UIKit)
- NumberKeypadDecimalPoint *numberKeyPad; in the interface
- the property before the @end
in the .m file i added:
-@synthesize of my labels, my fields and “numberKeyPad”
- i did “copy and past” of textFieldShouldBeginEditing, textFieldDidBeginEditing and textFieldDidEndEditing.
when i tap on my text fileds, appear the normal number pad.
i’m sure that the label are connected with the class: i can use their contents to calculate and display the result.
can you help me to implement your code?
thanks a lot!
pierre
pierre |
sorry…
i solved myself…
Abhi |
Hi,
I am adding this button to the keyboard when the phone is in Landscape orientation. No event i fired when I click on left part of the button. But if I click on right side or center it works. Anyone know why this is so???
- Abhi
Abhi |
Sorry,
This issue is not for this particular button but it is applied to all the buttons in the left pane of the keyboard. Does anyone know about this?
- Abhi
admin |
@Mawa
There is a bug in XCode/Interface builder and it doesn’t show UIKeyboardTypeDecimalPad as an option. You can set it in your code in viewDidLoad() or somewhere else suitable.
StatCoder |
What happens if the device is using an OS less than 4.1? Will it just show a numeric keypad without the decimal? Also, this still isn’t the ideal numeric keypad. It still is arrange in the telephone keypad order with “1″ in the top left rather than the traditional calculator or keyboard numeric keypad with the “1″ in the lower left. It also has the letters from the telephone keypad.
Anthony D |
Hey, just wanted to say thanks for posting your work to show guys like me how to solve this problem. Thanks!!!
Mohamed Barakat |
Thank you very much works like a charm
Ann |
Thank you for your decimal point code. I modified it to enter a negative sign. It operates perfectly, but for some reason the button background image doesn’t change. Any ideas?
Thanks again.
Ann |
Never mind. I imported the png incorrectly and somehow messed it up.
Deryck |
Thanks for the very helpful post.
I believe that you can update to use -(void)setNeedsDisplay;
- (void)drawRect:(CGRect)rect {
//[super drawRect:rect];
[super setNeedsDisplayInRect:rect];
….
}
cheers
JR |
Genius code!!!! Congratz…
Is it possible to make this code work on iPad app ?
Ernest |
First off, thanks for posting this great code. One problem is when the keyboard pops up, the view no longer shifts up (via the keyboardWillShow notifications). Is there something that needs to be changed to react to this ‘new’ number pad?
Thanks again.
Javi |
Thank´s a lot!!!!
Almost drag and drop.
)
Pingback: Actualización a iOS SDK 4.1 | Carlos Faria
dong hyy |
thanks for share!
Pingback: Add the Return Key for UIKeyboardTypeNumberPad in iPhone SDK 4 | Iphone Dev Center
William Biggy |
Nice thank you it’s still work on ios 4.3 and 5.0
William Biggy |
Sorry for the mistakes
admin |
Nice to know that it’s still useful.
Rad |
cool idea, but …
#1 what about the landscape mode ? the dot is not visible.
#2 for me, the dot also shows up in other types of input fields that are in the same view (ie Numbers and Punctuations).
cheers
Imran Raheem |
From OS 4.1 & above, the Decimal Keyboard is now available in the SDK.
Try this:
yourTextField.keyboardType = UIKeyboardTypeDecimalPad;
BizAffinchece |
Today I found differences in the Internet unburden of ads – Boardfreeads.com. Enormous design, amiably intellect usability. And most importantly, at liberty ads and articles on all the topics that worth you. It should be added, the situation offers international.
Who knows everywhere these [url=http://boardfreeads.com/]free ads[/url]?
Nilesh |
I have used the code to show a DONE button on the numeric keypad. It works perfectly fine till iOS 4.3. However, I am facing a strange problem using this code in iOS 5.0. For any textfield having a numeric keypad, if there are any validations done and if we get an Alertview. Then once the focus goes back to the same text field the Keypad comes up without the DONE button (i.e. DONE button disappears) and strangely this happens in iOS 5.0 only. Could you please provide some inputs on this ? It would be of great help.
Livegix |
Just figured out, that there is a bug since iOS4.3. if Emoji Keyboard is enabled. If you use Emoji keyboard in i.e. messages and leave it (don`t change it back to normal), the Emoji Keyboard appears as well in the App where the NumberKeyPad usually should appear. Has somebody maybe already a fix for that? Thank´s in advance, Best regards, Jens
Pingback: home cellulite treatment
smartphone tips |
I just like the valuable information you provide for your articles. I’ll bookmark your weblog and test again right here regularly. I’m somewhat certain I’ll be told many new stuff right here! Best of luck for the following!
Pingback: copic markers cheap ebay
Pingback: copic markers in canada
Ben Reynolds |
Thanks for the tip — very helpful!
Wilb |
Livegix i agree !! have you found a solution for that ?