You can specify the behavior in Interface Builder of search fields, like most Cocoa controls, without writing any source code. This is described in “Adding a Search Field to Your Application.” If you want to change a search field’s appearance, for example, its buttons or field locations, this article describes how.
Customizing Buttons
Customizing Field Locations
In general, avoid changing the search button cell unless you want a custom appearance or behavior. Typically, the search button has two behaviors: it either displays a menu (if a menu template is set), or it invokes the search field’s action method (when the button is clicked). The cancel button appears only if there is text in the search field.
You change the search button cell by sending setSearchButtonCell:
to the button’s cell, and change the cancel button cell by sending setCancelButtonCell:
to the button’s cell. To remove the search button or the cancel button, set the cell for that button to nil
.
If you have customized either the search and cancel buttons, you can easily “reset” their attributes to the search field cell's default image, alternate image, target, and action. You do this for the search button by sending resetSearchButtonCell
to the button’s cell, and for the cancel button by sending resetCancelButtonCell
to the button’s cell. All other attributes of the search field cell are not changed.
The following NSSearchFieldCell methods return the bounds for the buttons and the text field. Override these methods to position the items differently. NSSearchFieldCell assumes that there is a search button, text field, and cancel button, from left to right.
- (NSRect) searchButtonRectForBounds:(NSRect)rect; |
- (NSRect) cancelButtonRectForBounds:(NSRect)rect; |
- (NSRect) searchTextRectForBounds:(NSRect)rect; |
© 2003, 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-02-08)