Eduardo
Eduardo

Reputation: 109

How to select all text in a UITextField using Swift

I'm using below code, but for some reason it's not working. Any idea?

textField.text = "Hello"

textField.becomeFirstResponder()

textField.selectedTextRange = self.textField.textRangeFromPosition(self.textField.beginningOfDocument, toPosition: self.textField.endOfDocument)

This is what I get:

This is what I get:

Upvotes: 10

Views: 9793

Answers (6)

yehyatt
yehyatt

Reputation: 2414

Update for swift 4 from 2017

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

Upvotes: 4

user20293252
user20293252

Reputation: 21

If the previously solutions doesn't help try this

    func textFieldDidBeginEditing(_ textField: UITextField) {
      if textField.text == "Untitled" {
          DispatchQueue.main.async {
              textField.selectAll(nil)
          }
       }
    }

Upvotes: -2

ZHZ
ZHZ

Reputation: 2098

Hope this works.

func textFieldDidBeginEditing(textField: UITextField) {
    textField.selectedTextRange = self.textField.textRangeFromPosition(self.textField.beginningOfDocument, toPosition: self.textField.endOfDocument)
}

Upvotes: 4

Petar Stoenchev
Petar Stoenchev

Reputation: 1

My answer is

Hello, I make with this


  override func selectAll(_ sender: Any?) {
  textView.selectAll(sender)
  }

Upvotes: -1

Anton
Anton

Reputation: 4018

None of the other answers worked for me. It turned out that I needed to place my code in viewDidAppear instead of viewDidLoad:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    textField.becomeFirstResponder()
    textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument) 
}

There are also some additional ways to select all text that others may find helpful:

// Select all without showing menu (Cut, Copy, etc)
textField.selectAll(nil)

// Select all and show menu
textField.selectAll(self)

Upvotes: 9

Cauê Jannini
Cauê Jannini

Reputation: 551

I had the same problem and I solved like this:

First, implement UITextFieldDelegate in your ViewController

class ViewController :  UITextFieldDelegate

Then, set your textField delegate to "self"

textField.delegate = self

Then, add this function to your ViewController class

func textFieldDidBeginEditing(textField: UITextField) {
    textField.selectedTextRange = textField.textRangeFromPosition(textField.beginningOfDocument, toPosition: textField.endOfDocument)
}

If you are already using the ViewController as delegate for other TextFields that you don't want to behave this way, you can just use a switch, like this:

func textFieldDidBeginEditing(textField: UITextField) {
    switch textField {
         case yourTextField:
              textField.selectedTextRange = textField.textRangeFromPosition(textField.beginningOfDocument, toPosition: textField.endOfDocument)
              break;

         case default:
              break;
    }
}

Upvotes: 8

Related Questions