How to Select All Text in a Uitextfield Using Swift

Select all text in a UITextField using Swift

textField.becomeFirstResponder()
textField.selectAll(nil)

Select all text in a UITextField

If you only aim to select all the text in the UITextField,
use this:

textField.selectAll(nil)

Programmatically Select all text in UITextField

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.

Select all text in TextField upon click SwiftUI

SwiftUI Solution:

struct ContentView: View {
var body: some View {
TextField("Placeholder", text: .constant("This is text data"))
.onReceive(NotificationCenter.default.publisher(for: UITextField.textDidBeginEditingNotification)) { obj in
if let textField = obj.object as? UITextField {
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
}
}
}
}

Note : import Combine


Use UIViewRepresentable and wrap UITextField and use textField.selectedTextRange property with delegate.

Here is the sample demo

struct HighlightTextField: UIViewRepresentable {

@Binding var text: String

func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.delegate = context.coordinator
return textField
}

func updateUIView(_ textField: UITextField, context: Context) {
textField.text = text
}

func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}

class Coordinator: NSObject, UITextFieldDelegate {
var parent: HighlightTextField

init(parent: HighlightTextField) {
self.parent = parent
}

func textFieldDidBeginEditing(_ textField: UITextField) {
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
}
}
}




For macOS

struct HighlightTextField: NSViewRepresentable {

@Binding var text: String

func makeNSView(context: Context) -> CustomTextField {
CustomTextField()
}

func updateNSView(_ textField: CustomTextField, context: Context) {
textField.stringValue = text
}
}

class CustomTextField: NSTextField {
override func mouseDown(with event: NSEvent) {
if let textEditor = currentEditor() {
textEditor.selectAll(self)
}
}
}

Swift Select all text of a UITextfield from UITableviewCell Class

In cellForRowAtIndexPath set the delegate of textField with current ViewController and try to use textFieldDidBeginEditing delegate method of UITextField.

func textFieldDidBeginEditing(textField: UITextField) {
textField.selectAll(nil)
}

Note: Don't forgot to set the textField delegate inside cellForRowAtIndexPath like this cell.textField.delegate = self

How to make UITextField select all first and then edit programatically?

This prevents a popover from appearing, when the user taps on the selected text.

To allow the first tap to always select all the text, and have the second tap deselect, keep your current code in the textFieldDidBeginEditing method, and extend UITextField to override canPerformAction:withSender:, to prevent the popover from appearing, like so:

UITextField Subclass

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender {
/* Prevent action popovers from showing up */

if (action == @selector(paste:)
|| action == @selector(cut:)
|| action == @selector(copy:)
|| action == @selector(select:)
|| action == @selector(selectAll:)
|| action == @selector(delete:)
|| action == @selector(_define:)
|| action == @selector(_promptForReplace:)
|| action == @selector(_share:) )
{
//Get the current selection range
UITextRange *selectedRange = [self selectedTextRange];

//Range with cursor at the end, and selecting nothing
UITextRange *newRange = [self textRangeFromPosition:selectedRange.end toPosition:selectedRange.end];

//Set the new range
[self setSelectedTextRange:newRange];

return NO;
} else {
//Handle other actions?
}

return [super canPerformAction:action withSender:sender];
}

UITextFieldDelegate Methods

//Select the text when text field receives focus
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[textField selectAll:nil];
}

//Hide the keyboard when the keyboard "Done" button is pressed
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];

return TRUE;
}

I hope that helps!



Related Topics



Leave a reply



Submit