I want to know the current input character the user just inputted.Comparing the old and the new input string seems to work, but it must be the last thing I'd like to try.Any suggestion? I guess there are some methods in iOS SDK that can do this in a better way.
With numberPad and avoid pasting, no one could guarantee there’s no other method that could accidentally key in non number into the UITextField. To make it even robust, we should now filter all non number inputs away.
The UITextField class does not provide built-in support for formatting its string using an Formatter object, but you can use the text field’s delegate to format the content yourself. To do so, use the text field’s delegate methods to validate text and to format it appropriately.
To have a simple way of accepting text input from user in an iOS App, we could use UITextField. By default it received string (alphabet, number, symbols etc). In order to make it receive number (positive number including zero), it is harder than it sound, as there’s much consideration needed.
After restricting it to number pad only, there’s another way to secretly enter text into it; that’s using Copy-Paste function. Long press on the UITextField would allow the Paste function to show up as below
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
from the UITextFieldDelegate should help you.
It is not only called for replacing text, also whenever the user presses a key on keyboard. (length of range will be 0 then and the location will be the current insertation position).
See the documentation for more information: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/doc/uid/TP40006991-CH3-SW9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With