views:

969

answers:

3

Is there a way to prevent the user from moving the cursor in a UITextField? I'd like it to stay at the end of the string.

A: 

I don't know that there's a built-in way to do it...perhaps you could subclass UITextField and handle the touch events yourself?

Jonas
+1  A: 

There's no way to prevent them from moving the cursor. You can, however, prevent them from editing the text except at the end by implementing the

– textField:shouldChangeCharactersInRange:replacementString:

method in your text field's delegate.

Edit: you can also set userInteractionEnabled to NO so that the user can't tap the field. Call becomeFirstResponder manually so that the field gets focus since the user can't tap to focus.

Adam Ernst
That is what I have now but it's kinda wonky for the user when they move the cursor and then type a key and the characters append to the end of the string. Not ideal...but not fatal. Looking for an easy way to disable touches on the UITextField. May go the subclass route if that's my only option.
Meltemi
May I ask what you're trying to accomplish? Why not allow placing the cursor?But yeah, it seems just setting userInteractionEnabled=NO would be the best fix.
Adam Ernst
A: 

I haven't check if you can disable the magnifying glass, but the recommended way to selectively disable editing behavior in a UIResponder (and thus a UITextField) is to implement canPerformAction:withSender of UIResponder.

See UIResponder documentation.

Maybe if you return "NO" for the select and selectAll action you can disable it.

Another, more brutal, way is to intercept any touch event and reset the cursor to the end.

Christian Fries