Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect the current input character in UITextField(iOS)

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.

like image 502
anna Avatar asked Apr 20 '12 14:04

anna


People also ask

Is it possible to avoid non number input in uitextfield?

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.

How do I format a string in uitextfield?

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.

How to accept text input from user in iOS app?

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.

How to secretly enter text into uitextfield in Microsoft Word?

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


Video Answer


1 Answers

- (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

like image 197
Thyraz Avatar answered Sep 30 '22 14:09

Thyraz