Quote:
Originally Posted by nacho4d
In case of UITextView I think can calculate the actual position by a function of the tapped position, and current font sizes. But I don't know how accurate this method can be. I case of UITextField I am not sure whether I can do it.
|
Normally you don't need to know the cursor position ahead of the keypress. The cursor position becomes available to you when the key is pressed. So assuming you can generate a real keypress event instead of using the method from the example, do the usual thing...
Just implement a UITextFieldDelegate and in particular the method:
Code:
textField:shouldChangeCharactersInRange:replacementString:
The range corresponds to the current cursor position and the string corresponds to the pressed key. If you want to allow the keypress at the given cursor position, return YES. If you don't like it, just return NO. If you want to substitute something in place of the pressed key, use something like
Code:
[textField setText:[ textField.text stringByReplacingCharactersInRange:range
withString:@"Q" ] ];
and return NO.
Of course, since there's no publicly documented way to generate a "keypress" event then I guess you may still be stuck!
Oh wait, never mind... In just poking around, I see there's a handy method/property if you use a UITextView instead of a UITextField:
Code:
[myTextView selectedRange]
That may be helpful, if you can dress up the text view to look more like a text field, and add the clear button to it, and so forth.... I still don't understand why this method isn't available in a text field. It's obviously very handy.