views:

1558

answers:

3

How can I programatically select all text in UITextField?

Thanks!

+1  A: 

If you mean how would you allow the user to edit the text in a uitextfield then just assign firstResponder to it:

[textField becomeFirstResponder]

If you mean how do you get the text in the uitextfield than this will do it:

textField.text

If you mean actually select the text (as in highlight it) then this will may be useful:

selectAll

ennuikiller
Actually I want to select all the text programatically (highlight), the same as when user taps once on text and then taps on "Select All"
Mirko Tomić
The last method is what you want then.
Kendall Helmstetter Gelner
Actually not:This method is invoked when the user taps the Select All command of the editing menu.What I want is to invoke Select All, like if the user tapped Select All command.
Mirko Tomić
A: 

Unfortunately I don't think you can do that.

I'm not sure if this helps you, but setClearsOnBeginEditing lets you specify that the UITextField should delete the existing value when the user starts editing (this is the default for secure UITextFields).

Dan J
+2  A: 

I just tested this to verify Mirko's comment above, but my test verifies that selectAll: does in fact select all the text when it's sent to the UITextField itself.

Note that the text will be immediately obscured with CUT | COPY | PASTE actions, but to your question, it is exactly what appears when a user taps "Select All" to begin with.

The solution I'm going with follows, note that the second line will temporarily hide the CUT/COPY/PASTE dialog, without disabling it for explicit user selections

[_myTextField selectAll:self];
[UIMenuController sharedMenuController].menuVisible = NO;
Justin Searls