How can I programmatically select all text in UITextField?
12 Answers
Thats what did the trick for me:
[self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];
Pretty ugly but it works, so there will be no sharedMenuController shown!
To fix the "only works every second time" problem use following:
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
UITextRange *range = [strongSelf textRangeFromPosition:strongSelf.beginningOfDocument toPosition:strongSelf.endOfDocument];
[strongSelf setSelectedTextRange:range];
});
Thanks to Eric Baker ( just edited from comment in here )

- 33,281
- 23
- 160
- 191

- 1,001
- 1
- 8
- 10
-
3This is actually a far superior answer to the question. It does what is asked for without any silly workarounds to prevent the menu being shown (which don't work for me anyway). – PKCLsoft Aug 16 '13 at 13:39
-
1Sorry to bring this back, but your solution works only the first time. The second time, it failed. Basically, what I did was that when users click on UITextField, select all text so they can override all of it, they can dismiss the keyboard by clicking something else (a background transparent in this case), after that clicking the same UITextField does not do the trick anymore. Repeat this process, I notice that it works for the odd time but does not work for the even time. I.e: works for first, third, fifth ; does not work for second, fourth, sixth. – L N Aug 30 '13 at 06:10
-
11@LeonNguyen This works for me to solve the problem you describe: `__weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ __strong __typeof(weakSelf) strongSelf = weakSelf; UITextRange *range = [strongSelf textRangeFromPosition:strongSelf.beginningOfDocument toPosition:strongSelf.endOfDocument]; [strongSelf setSelectedTextRange:range]; });` – Eric Baker Sep 17 '13 at 01:45
-
@EricBaker I can confirm that your solution worked. It seems to be an issue with threading. Thanks – L N Sep 19 '13 at 01:35
-
This *IS* the answer. – Mugunth Jan 14 '14 at 04:28
-
If this doesn't work for you, try calling `[self.titleField becomeFirstResponder];` first so that your text field will get cursor focus and the keyboard will appear. – Andrew Feb 22 '14 at 07:17
-
is it possible to hide the blue lines (the ones for adjusting the selection)? – dot Feb 24 '14 at 21:29
-
1The __weak / __strong hack didn't work for me. I had to deselect all text upon endEditing so it could be freshly selected upon startEditing. – marsbear Sep 03 '15 at 20:22
-
@LeonNguyen please provide your code __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ __strong __typeof(weakSelf) strongSelf = weakSelf; UITextRange *range = [strongSelf textRangeFromPosition:strongSelf.beginningOfDocument toPosition:strongSelf.endOfDocument]; [strongSelf setSelectedTextRange:range]; }); in swift – Sujatha Girijala Dec 26 '16 at 14:14
-
The `DispatchQueue.main.async` here was the key for me, without it, menu always showed up – RyanM Aug 22 '20 at 00:39
Turns out, calling -selectAll: with a non-nil sender displays the menu. Calling it with nil causes it to select the text, but not display the menu.
I tried this after my bug report about it came back from Apple with the suggestion that I pass nil instead of self.
No need to muck with UIMenuController or other selection APIs.

- 3,298
- 3
- 29
- 47
-
3I've been trying to figure out how to select the text without displaying the menu. Thanks adding that tid-bit. – Erik Sep 01 '15 at 14:21
-
1
-
3Forget it, for alert controllers this answer works: http://stackoverflow.com/a/35992115/433373 – Nicolas Miari Apr 21 '16 at 06:36
-
1+1 for proper answer. Another +1 for mentioning about sender and menu. Another +1 for mentioning source of information. Sadly, I have only one vote... – eonil Sep 11 '18 at 00:52
Use what you need
ObjC
[yourtextField becomeFirstResponder]; //puts cursor on text field
[yourtextField selectAll:nil]; //highlights text
[yourtextField selectAll:self]; //highlights text and shows menu(cut copy paste)
Swift
yourTextField.becomeFirstResponder() //puts cursor on text field
yourTextField.selectAll(nil) //highlights text
yourTextField.selectAll(self) //highlights text and shows menu(cut copy paste)
-
3Best answer here. Super simple, and the 'nil' trick to hide the Cut/Copy/Pate pop-up does the trick! – Marchy Oct 21 '14 at 18:04
-
1
-
8Note if you want to select text when first showing a view, you must place the `selectAll` call in `viewDidAppear`. It will not work when placed in `viewDidLoad` or `viewWillAppear`. – Benjamin Martin May 30 '15 at 04:53
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;

- 4,789
- 4
- 45
- 56
-
I want to select all text when the user taps the UITextField, so that as soon as they start typing, original text gets erased. Which Outlet action should I call to achieve this? I tried linking touch up inside to an IBAction which calls your code above but didn't work. – C0D3 Jan 04 '12 at 21:16
-
-
Just to let you know that it didn't work for me :( The menu is still not visible when we call `[UIMenuController sharedMenuController].menuVisible = NO;` so that line won't have any effect. – Fábio Oliveira May 17 '13 at 14:43
-
3Actually to select all the text so that the menu doesn't appear you have to use `setSelectedRange:` instead. – Fábio Oliveira May 17 '13 at 15:02
This is the best solution I've found. No sharedMenuController, and it works consecutively:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField performSelector:@selector(selectAll:) withObject:nil afterDelay:0.1];
}
-
1When adding an answer years after a question has been asked, would be good to explain *why* you are adding yet another answer. There are many existing answers here: under what conditions is your answer "better" than all of those? What is the benefit? Are there situations where it might not be beneficial, and what existing answer would you use then? – ToolmakerSteve Mar 14 '17 at 04:56
-
5Thank you! This approach, or rather the Swift equivalent "textField.perform(#selector(selectAll), with: nil, afterDelay: 0.01)", fixes the issue Leon Nguyen was having with blackforestcowboy's answer above, where it only works every other time. Something about delaying the action must sort out the threading. As @ToolmakerSteve says, it would be useful to explain that in the answer here. – Andy Mortimer Jul 05 '17 at 20:19
-
1Not sure if prettier, but without selector(s) I've been able to get the menu to NOT show up by calling: `DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: { textField.selectAll(nil) })` – Rock_Artist Sep 24 '22 at 07:51
-
Used the suggestion from @Rock_Artist with [SwiftUI-Introspect](https://github.com/siteline/SwiftUI-Introspect) in my SwiftUI project. Worked great. – SPM Jun 29 '23 at 03:53
Swift
Select all text in a UITextField
:
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
My full answer is here: https://stackoverflow.com/a/34922332/3681880
-
BTW, if there is a Swift equivalent to the shorter alternative solution `[textField selectAll:nil]`, would be good to list that also. – ToolmakerSteve Mar 14 '17 at 04:52
-
To be able to select text, the text field has to be editable. To know when the text field is editable use the delegate methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
I don't think textFieldShouldBeginEditing: is required but it's what I used in my implementation.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[textField selectAll:textField];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
Passing in nil to selectAll: won't show the Menu.

- 39
- 1
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
).

- 1,042
- 8
- 17

- 25,433
- 17
- 100
- 173
I create a custom alert view which contains a UITextField
inside. I found a problem to the textfield is that: beginningOfDocument
only has value if textfield is added to screen & becomeFirstResponder
is called.
Otherwise beginningOfDocument
returns nil
and [UITextField textRangeFromPosition:]
can not get the value.
So here is my sample code to solve this case.
UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
[window addSubview:theAlertView]; // textfield must be added as a subview of screen first
UITextField *textField = theAlertView.textField;
[textField becomeFirstResponder]; // then call to show keyboard and cursor
UITextRange *range = [textField textRangeFromPosition:textField.beginningOfDocument toPosition:textField.endOfDocument]; // at this time, we could get beginningOfDocument
[textField setSelectedTextRange:range]; // Finally, it works!!!

- 7,745
- 3
- 38
- 40
UITextField *tf = yourTF;
// hide cursor (you have store default color!!!)
[[tf valueForKey:@"textInputTraits"] setValue:[UIColor clearColor]
forKey:@"insertionPointColor"];
// enable selection
[tf selectAll:self];
// insert your string here
// and select nothing (!!!)
[tf setMarkedText:@"Egor"
selectedRange:NSMakeRange(0, 0)];
Done!

- 4,778
- 1
- 24
- 32
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:

- 37,241
- 25
- 195
- 267

- 46,381
- 14
- 112
- 137
-
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 Nov 07 '09 at 01:17
-
-
1Actually 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 Nov 07 '09 at 09:20