0

I am using swift in a storyboad application, and I would like it to, when a user enters the box (IE, when it becomes the first responder) to enter text it highlights all the text. What is done in most web browsers. Here is what I have tried, which has not worked:

    func textFieldDidBeginEditing(_ textField: UITextField) {
//      textField.selectAll(nil)
        print("click 2")
//      textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        print("click 1")
//      textField.selectAll(nil)
//      textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
        return true
    }

Making a button that runs this code does actually work, but I want it to be when you click on the field. Making an invisible button on top seems like a bad idea. I've tried other variations of this as well, but none have worked for me.

UPDATE: What I've ended up doing was making an invisible button on top of the existing text field, and then making that select the text. It may be a little janky but it works perfectly so I have no problems with that solution anymore. Extra code:

    @IBAction func seachBtnClicked(_ sender: Any) {
        //The invisible button over the search bar has been clicked
        search.selectAll(nil)
        searchBtn.isHidden = true
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //Return has been pressed, so we are going to load a new page
        processInput()
//This does mean that it only comes back if you press enter, 
//but right now I think that's how I want it. 
//Could change it to be in the textFieldDidEndEditing() function to change that 
        searchBtn.isHidden = false 
        return true
    }
pbrink21
  • 13
  • 3
  • Does this answer your question? [Select all text in TextField upon click SwiftUI](https://stackoverflow.com/questions/67502138/select-all-text-in-textfield-upon-click-swiftui) – Suyash Medhavi Sep 07 '22 at 18:36

2 Answers2

0

You are probably just missing the textField.delegate = self for your textfield. The first of the two functions, func textFieldDidBeginEditing(_ textField: UITextField) is the one that is always performed as soon as a textfield is tapped on to focus on it. Try adding yourTextField.delegate = self in your viewDidLoad function and see if that works out.

Nicop.dev
  • 50
  • 1
  • 8
  • I do already have that in there. The functions themselves are triggered (the print statements go off), and running those lines for selecting works when I set up a button to do it, they just don't work in those functions it seems. – pbrink21 Sep 06 '22 at 16:21
0

What I've ended up doing was making an invisible button on top of the existing text field, and then making that select the text. It may be a little janky but it works perfectly so I have no problems with that solution anymore. Extra code:

    @IBAction func seachBtnClicked(_ sender: Any) {
        //The invisible button over the search bar has been clicked
        search.selectAll(nil)
        searchBtn.isHidden = true
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //Return has been pressed, so we are going to load a new page
        processInput()
//This does mean that it only comes back if you press enter, 
//but right now I think that's how I want it. 
//Could change it to be in the textFieldDidEndEditing() function to change that 
        searchBtn.isHidden = false 
        return true
    }
pbrink21
  • 13
  • 3