Text fields, text views, and the field editor are important objects in the Cocoa text system because they are central to the user’s interaction with the system. They provide text entry, manipulation, and display. If your application deals in any way with user-entered text, you should understand these objects.
A text field is a user interface control object instantiated from the NSTextField class. Figure 1 shows a text field. Text fields display small amounts of text, typically (although not necessarily) a single line. Text fields also provide places for users to enter text responses, such as search parameters. Like all controls, a text field has a target and an action. By default, text fields send their action message when editing ends—that is, when the user presses Return or moves focus to another control. You can also control a text field’s shape and layout, the font and color of its text, background color, whether the text is editable or read-only, whether it is selectable or not (if read-only), and whether the text scrolls or wraps when the text exceeds the text field’s visible area.
To create a secure text field for password entry, you use NSSecureTextField, a subclass of NSTextField. Secure text fields display bullets in place of characters entered by the user, and they do not allow cutting or copying of their contents. You can get the text field’s value using the stringValue
method, but users have no access to the value.
The usual way to instantiate a text field is to drag an NSTextField object from the Cocoa-Views palette in Interface Builder and place it in a window of your application’s user interface. Then, if you then want to convert the text field to a secure text field, you select it, open the Info window (Command-Shift-I), choose the Custom Class pane (Command-5), and select NSSecureTextField.
See Text Fields more information.
Text views are user interface objects instantiated from the NSTextView class. Figure 2 shows a text view. Text views typically display multiple lines of text laid out in paragraphs with all the characteristics of sophisticated typesetting. A text view is the main user interface to the Cocoa text-editing system. It handles user events to provide text entry and modification, and to display any font, including those of non-English languages, with arbitrary colors, styles, and other attributes.
The Cocoa text system supports text views with many other underlying objects providing text storage, layout, font and attribute manipulation, spell checking, undo and redo, copy and paste, drag and drop, saving of text to files, and other features. NSTextView is a subclass of NSText, which is a separate class for historical reasons. You don’t instantiate NSText, although it declares many of the methods you use with NSTextView. When you put an NSTextView object in an NSWindow object, you have a full-featured text editor whose capabilities are provided “for free” by the Cocoa text system. (See “Building a Text Editor in 15 Minutes” for more information.)
The field editor is a single NSTextView object that is shared among all the controls, including text fields, in a window. This text view object inserts itself into the view hierarchy to provide text entry and editing services for the currently active text field. When the user shifts focus to a text field, the field editor begins handling keystroke events and display for that field. The field editor designates the current text field as its delegate, enabling the text field to control changes to its contents. When the focus shifts to another text field, the field editor attaches itself to that field instead. Figure 3 illustrates the disposition of the field editor in relation to the text field it is editing.
Because only one of the text fields in a window can be active at a time, the system needs only one NSTextView instance per window to be the field editor. Among its other duties, the field editor maintains the selection. Therefore, a text field that's not being edited typically does not have a selection at all. (However, developers can substitute custom field editors, in which case there could be more than one field editor.)
For more information about the field editor, see “Working With the Field Editor.”
Last updated: 2009-11-30