skip to Main Content

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
    }

2

Answers


  1. Chosen as BEST ANSWER

    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
        }
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search