Programmatically Select all text in UITextField

IosSwiftUitextfieldUikit

Ios Problem Overview


How can I programmatically select all text in UITextField?

Ios Solutions


Solution 1 - Ios

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 )

Solution 2 - Ios

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.

Solution 3 - Ios

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)

Solution 4 - Ios

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;

Solution 5 - Ios

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];
}

Solution 6 - Ios

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

Solution 7 - Ios

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.

Solution 8 - Ios

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

Solution 9 - Ios

Swift 3:

textField.selectAll(self)

Solution 10 - Ios

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!!!

Solution 11 - Ios

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!

Solution 12 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMirkoView Question on Stackoverflow
Solution 1 - IosblackforestcowboyView Answer on Stackoverflow
Solution 2 - IosRickView Answer on Stackoverflow
Solution 3 - IosTedView Answer on Stackoverflow
Solution 4 - IosJustin SearlsView Answer on Stackoverflow
Solution 5 - IosJase68View Answer on Stackoverflow
Solution 6 - IosSuragchView Answer on Stackoverflow
Solution 7 - IosJason DanielsView Answer on Stackoverflow
Solution 8 - IosDan JView Answer on Stackoverflow
Solution 9 - IosJohn ScaloView Answer on Stackoverflow
Solution 10 - Iosnahung89View Answer on Stackoverflow
Solution 11 - IosSound BlasterView Answer on Stackoverflow
Solution 12 - IosennuikillerView Answer on Stackoverflow