8

I want to clear the selection of NSTextView after it loses focus. How can I achieve that? Thanks in advance!

Kai.

nonamelive
  • 6,510
  • 8
  • 40
  • 47

1 Answers1

11

Set up a delegate object (or use your app delegate) that conforms to NSTextDelegate protocol. Then all you need to do is to implement textDidEndEditing: to clear the selection. From the docs:

textDidEndEditing:

Informs the delegate that the text object has finished editing (that it has resigned first responder status).

- (void)textDidEndEditing:(NSNotification *)aNotification

Something like:

#ifndef NSZeroRange
#define NSZeroRange NSMakeRange(0,0)
#endif

- (void)textDidEndEditing:(NSNotification *)aNotification {

    [myTextView setSelectedRange:NSZeroRange];
}
sidyll
  • 57,726
  • 14
  • 108
  • 151