NonSelectingTokenField is a simple Objective-C / Cocoa subclass of NSTokenField that avoids selecting the tokens upon resigning first responder status or completing editing. This class also serves as a demonstration of how to manipulate the selection of any NSTextField subclass (of which NSTokenField is).


The class overrides -didBecomeFirstResponder and -textDidEndEditing: and manipulates the selected range of the token field’s associated field editor.

The class simply selects nothing (a zero-length range), placing the insertion caret at the end of the token field’s contents (a location of [[fieldEditor string] length]).


Just drop the class into your project. In Interface Builder, open the XIB(s) containing the token fields whose behavior you wish to change, then change their class name to NonSelectingTokenField. Build, run, and enjoy the wonder that is Cocoa.


This source code is free to use in any private, public, or commercial software products with no restrictions.


Download Source

© Joshua Nozzi. All rights reserved.