tags:

views:

478

answers:

2

I have a UITextField that I'm forcing formatting on by modifying the text inside the change notification handler. This works great (once I solved the reentrancy issues) but leaves me with one more nagging problem. If the user moves the cursor someplace other than the end of the string then my formatting change moves it to the end of the string. This means users cannot insert more than one character at a time into the middle of the text field. Is there a way to remember and then reset the cursor position in the UITextField?

+1  A: 

I don't think there is a way to place the cursor at a particular place in your UITextField (unless you got very tricky and simulated a touch event). Instead, I'd handle formatting when the user has finished editing their text (in textFieldShouldEndEditing:) and if their entry is incorrect, don't allow the text field to finish editing.

pix0r
I had considered that but what I'm implementing is similar to the way you enter numbers at an ATM. The decimal is always there and as you type numbers they move to the left so entering 1-0-5-0 results first in 0.01, then 0.10, 1.05, 10.50. To do it at the end would allow 1050 in the field and then change it to 10.50 which is rather confusing to the user.
Jeremy
A: 

I've finally found a solution for this problem! You can put the text you need inserted into the system pasteboard and then paste it at the current cursor position:

[myTextField paste:self]

I found the solution on this person's blog:
http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html

The paste functionality is OS V3.0 specific, but I've tested it and it works fine for me with a custom keyboard.

Dan J