The NSText​Finder class provides an optional search and replace find-bar interface inside a view, usually a scroll view.


The class serves as a controller for the standard Cocoa find bar. The NSText​Finder class interacts heavily with a client object which supports the NSText​Finder​Client protocol. The client object provides access to the content being searched and provides visual feedback for a search operation.

All menu items related to finding (Find…, Find Next, Find Previous, Use Selection for Find, etc.) should have the same action, perform​Text​Finder​Action(_:​), which gets sent down the responder chain in the standard method.

Implementing a Find Bar

A responder of perform​Text​Finder​Action(_:​) is responsible for creating and owning an instance of NSText​Finder. Before any other messages are sent to the NSText​Finder, you should set its client property to an object which implements the NSText​Finder​Client protocol.

Each user interface item used for finding has a different tag value , which corresponds to the appropriate value in NSText​Finder​Action. Upon receipt of the perform​Text​Finder​Action(_:​) message, the responder should call the following on its NSText​Finder instance:

- (void)performAction:(NSTextFinderAction)action;

This method will determine the desired action from the tag and make various callbacks to client to perform that action. These callbacks are defined in the NSText​Finder​Client protocol.

Validation occurs by a similar pattern. The responder should implement validate​User​Interface​Item(_:​) and, when the item's action is the perform​Text​Finder​Action(_:​) method, it should pass the item's tag to the following method and return the result:

- (BOOL)validateAction:(NSTextFinderAction)action;

This method will invoke the required client methods to determine what the appropriate response should be. These callbacks are also defined in the NSText​Finder​Client protocol.

When an action is performed, the text finder will ask its client for the string to search. There are two ways the client can provide this string. It can either implement the string method, and simply return an NSString instance, or it can implement the following methods: string(at:​effective​Range:​ends​With​Search​Boundary:​) and string​Length().

These methods make it possible to use the text finder with data that cannot be easily or efficiently flattened into a single string. In the first method, the client should return the string which contains the character at the given index in as conceptual concatenation of all strings that are to be searched.

For example, if a client had the strings “The quick”, “brown fox” , “jumped over the lazy”, and “dog”, and a NSText​Finder instance requests the string at index 18, then the client should return "brown fox”. If all the strings were concatenated together would be the ‘x’ in "fox". Additionally, the client should return, by-reference through the out​Range parameter, the effective range of the string that is returned. In this example, the range of “brown fox” is {9, 10}, because, in the imagined concatenation, the substring starts at index 9 and is 10 characters long.

In some cases, it is not desirable for a match to be found which overlaps multiple strings returned by the string(at:​effective​Range:​ends​With​Search​Boundary:​) method. For example, suppose a client has a list of names like “John Doe”, and “Jane Doe”, etc. Normally, if the string is concatenated together like so: "John Doe Jane Doe", then a search for "Doe Jane" would succeed. To prevent this often undesirable behavior, the client should return true, by-reference, through the out​Flag parameter, when returning each individual string. This indicates that there is a boundary between the current string and the next string that should not be crossed when searching for a match.

One of the two approaches (implementing the string method the string(at:​effective​Range:​ends​With​Search​Boundary:​) and string​Length() methods) must be implemented by the client or the NSText​Finder instance will not function. If all three methods are implemented, string(at:​effective​Range:​ends​With​Search​Boundary:​) will be used by default.

The text finder may require additional information from the client object to perform an action, or it may require the client to perform some parts of the action on its behalf. The methods and properties described in Text Finder Client Implementation Requirements describes the hooks the text finder requires for each of the actions it supports. If the client does not implement one of these methods or properties, then the action that requires it will be disabled.

Text Finder Client Implementation Requirements

The text finder’s client may implement the following properties for by the validate​Action(_:​) method: is​Selectable, allows​Multiple​Selection, and editable. If any of these properties is not implemented, a value of true is assumed. Returning false from any of these methods will disable the actions that require them. For more information about these properties see NSText​Finder​Client.

The following implementation’s are required by the text client to support the specified actions:

Incremental Search Support

A common feature used in conjunction with the find bar is incremental search. This feature allows users to find matches immediately as they are typing. In OS X v10.7, the text finder provides this feature for its clients with minimal additional API.

Incremental searching can be enabled by setting the is​Incremental​Searching​Enabled property true. This property alone is sufficient to start searching incrementally.

For improved responsiveness, when a document is sufficiently long, the text finder will search the document in the background. To ensure thread-safety, a client using incremental search must call the note​Client​String​Will​Change() method before any changes are made to the string provided to the text finder.

During incremental search, all visible matches are highlighted. If the client object that conforms to the NSText​Finder​Client protocol implements the read-only visible​Character​Ranges property, then by default a gray overlay will appear over your find bar container’s content view with transparent areas for each match. This view should be a superview of all subviews reported by the text finder client. The NSScroll​View class already implements this property, so you only need to implement this property when using a different container class.

Finally, this mode also requires the same two NSText​Finder​Client protocol methods that are used to display the find indicator: content​View(at:​effective​Character​Range:​) and rects(for​Character​Range:​) be implemented. However, the implementation of these methods is identical for both purposes, so no additional work is required to support incremental search.

Replacing Text

The text replacement methods:NSText​Finder​Action, find​Indicator​Needs​Update, and validate​Action(_:​) are used by the replace, replace all, replace all in section, and replace and find actions.

Before a replace operation is performed, the NSText​Finder instance calls the client object’s NSText​Finder​Action method to determine if a replacement should take place. If it returns false, then the characters in the given ranges will not be replaced. If the method returns true, or is not implemented, then the second method, find​Indicator​Needs​Update, instructing the client to carry out the replacement. Finally, validate​Action(_:​), if implemented, is invoked to indicate that the replacement was completed.

For replace all actions, these methods will be called multiple times, starting from the last match and moving toward the first, in order to preserve the indexes of the matches which precede the current one.

The Text Finder Container

In order to display the find bar, a container for the find bar must be specified. The container is an object that conforms to the NSText​Finder​Bar​Container protocol. You specify a find bar container using the following NSTextFinder class’s find​Bar​Container property.

When a new NSTextFinder instance is created and instructed to display the find bar, it will create a view for the find bar and assign that to the container via the NSText​Finder​Bar​Container class’s find​Bar​View property. The container then owns that view and should release it when the container is deallocated. The container should make the find bar visible when the container’s is​Find​Bar​Visible property is set to true. The container should implement the find​Bar​View​Did​Change​Height() method so it can reposition the find bar when its height changes, usually in response to user action.

Implementation by AppKit Classes

Two AppKit classes already provide support for the NSText​Finder class, including: the NSScroll​View and NSText​View classes.

Scroll View Support for the Find Bar

The NSScroll​View class conforms to NSText​Finder​Bar​Container protocol in order to support the find bar for any document view searched by the find bar. The find bar can be positioned either above or below the document view by assigning one of the values of the NSScroll​View​Find​Bar​Position constants to the find​Bar​Position property.

Text View Support for the Find Bar

The NSText​View class also supports the find bar. The find bar can be enabled or disabled on a text view with the uses​Find​Bar property.

Since OS X v10.5, the NSText​View class has used an animated find indicator to give feedback to the user about a successful find action. The find indicator could be activated manually on an text view via the show​Find​Indicator(for:​) method.

To provide this functionality for text finder clients in OS X v10.7, the NSText​Finder class shows the find indicator at the appropriate time automatically when performing a find. However, text finder needs to know where to show the indicator and it needs assistance in drawing the text contents of the find indicator. During incremental search the NSText​Finder class needs also to know the locations of all the visible matches so it can highlight them as well. The following method additions to the NSText​Finder​Client protocol provide these capabilities: content​View(at:​effective​Character​Range:​) and rects(for​Character​Range:​).

The content​View(at:​effective​Character​Range:​) method is called to determine what view the content is displayed in, that is, over which the find indicator will be displayed. Since content may potentially be spread over multiple views (like the NSLayout​Manager class , which supports multiple text views), the method asks for the view at a given index, and the full range of the string that is contained by that view.

In the rects(for​Character​Range:​) method, the client should determine and return the rectangles in which the content with the given range is displayed in its content​View. The given range is guaranteed not to span multiple content views. The returned rectangles tell the NSText​Finder instance where the matched range is found, so it can show the find indicator there.

The NSText​Finder and NSView classes will handle the find indicator correctly when the content​View() is resized, moved, or removed from the view hierarchy. If your content view's scrolling is done by an NSScroll​View object, the find indicator will also be handled for you during scrolling. Beyond these cases, there may be some circumstances where the find indicator should be immediately cancelled and hidden, such as when the view's content or selection is changed without the knowledge of the text finder . The find indicator can be cancelled using the NSText​Finder cancel​Find​Indicator() method. If your document is not scrolled by NSScroll​View, then you should set the find​Indicator​Needs​Update property to true.

NSText​Finder is responsible for drawing the yellow find indicator background bezel, but the view must provide the contents. NSText​Finder also sets up a drawing context and causes the content view to draw into it. There are two ways this can happen.

  • The NSText​Finder instance invokes the client object’s draw​Characters(in:​for​Content​View:​) method, if implemented. The client should then draw the requested characters (or ask the containing view to draw the characters, which is provided as a convenience) at the origin of the current context. If the requested character range partially intersects a glyph range, the client may draw the entire glyph, if necessary for performance.

  • If draw​Characters(in:​for​Content​View:​) method is not implemented, then the NSText​Finder instance uses the normal view drawing mechanisms. The content​View does not need take any additional action to make this happen

In OS X v10.7, the NSText​View class also provides incremental search support. It is disabled by default, but can be enabled by setting the is​Incremental​Searching​Enabled property to true. Also, because incremental searching requires the find bar, uses​Find​Bar must be set to true for incremental searching to be occur.


Initializing the Text Finder


Initializes and returns a new NSText​Finder instance.

Validating and Performing Text Finding

func perform​Action(NSText​Finder​Action)

Performs the specified text finding action.

func validate​Action(NSText​Finder​Action)

Allows validation of the find action before performing.

func cancel​Find​Indicator()

Cancels the find indicator immediately.

Getting the Find Bar Container

Getting and Setting the Find Bar Client

var client:​ NSText​Finder​Client?

The object that provides the target search string, find bar location, and feedback methods.

Noting Changes in the Original Content

func note​Client​String​Will​Change()

Invoke this method when the searched content will change.

Updating the Find Indicator

var find​Indicator​Needs​Update:​ Bool

Invoke to specify that the find indicator needs updating when not contained within a scroll view.

Incremental Search Configuration

class func draw​Incremental​Match​Highlight(in:​ NSRect)

Override this method to draw custom highlighting.

var incremental​Match​Ranges:​ [NSValue]

Array of incremental search matches posted on the main queue, which have been found during a background search.

var is​Incremental​Searching​Enabled:​ Bool

Determines if incremental searching is enabled.

var incremental​Searching​Should​Dim​Content​View:​ Bool

Determines the type of incremental search feedback to be presented



These constants specify the user interface item tags that correspond find action. These constants are passed to the perform​Text​Finder​Action(_:​) method, the responder will call the appropriate method for the tag. That method will, in turn, determine the desired action and invoke the appropriate method in the NSText​Finder object’s NSText​Finder​Client protocol.

Text Finder Options For The Pasteboard

The following keys are used for communicating NSText​Finder search options via pasteboard. Use the NSPasteboard​Type​Text​Finder​Options type


The following constants indicate the type of search anchor an action should perform.


Inherits From