Voloda2
Voloda2

Reputation: 12617

How do I deselect text in UITextView?

After some moment I need deselect text in UITextView. How do I deselect text in UITextView?

enter image description here

EDIT: I need to do programmatically.

Upvotes: 14

Views: 7478

Answers (3)

Alex Zanfir
Alex Zanfir

Reputation: 573

This is the way it works and you can touch wherever you want in the view in order to deselect the previously selected text:

On your UIViewController write this function:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    self.view.endEditing(true)
}

Upvotes: 3

Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

The previous answer didn't work for me, the compiler complains:

Sending 'void *' to parameter of incompatible type 'NSRange' (aka 'struct _NSRange')

I had to use an empty NSRange struct instead:

[self setSelectedRange:(NSRange){ .location = 0, .length = 0 }];

Upvotes: 2

rob mayoff
rob mayoff

Reputation: 386018

UITextView adopts the UITextInput protocol, which has a selectedTextRange property. Set the property to nil:

self.textView.selectedTextRange = nil;

Upvotes: 43

Related Questions