Search code examples
swiftuikituitextfield

textField.selectAll(nil) doesn't mark the text


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

enter image description here

I have an issue where if I click at the left part of the Text Field (where the arrow point), the text doesn't get marked. Instead, the marker is positioned "before" the first character (see pic).

It works if I click on the text.

How can the text always get marked independently of where user clicks on the textfield?


Solution

  • It appears to be related to using .textAlignment = .right

    Try it like this:

    func textFieldDidBeginEditing(_ textField: UITextField) {
        DispatchQueue.main.async {
            textField.selectAll(nil)
        }
    }