Select whole text of UITextView using Swift

1.8k Views Asked by At

I tried to do this the same way as I'm doing it with a UITextField:

textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.endOfDocument)

What am I doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

You should make the text field the first responder before selecting it:

textView.becomeFirstResponder()
textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.endOfDocument)

...or, better yet, just call selectAll:

textView.selectAll(self)