An NSTextField object is a kind of NSControl that displays text that the user can select or edit and that sends its action message to its target when the user presses the Return key while editing.


The NSTextField class uses the NSTextFieldCell class to implement its user interface.

The parent class, NSControl, provides the methods for setting the values of the text field, for example: stringValue, doubleValue, etc.. There are corresponding methods to retrieve values.


Controlling Selection and Editing

var isEditable: Bool

A Boolean that controls whether the user can edit the receiver’s text.

var isSelectable: Bool

A Boolean that determins whether the receiver is selectable (but not editable).

Controlling Rich Text Behavior

var allowsEditingTextAttributes: Bool

A Boolean that controls whether the receiver allows the user to change font attributes of the receiver’s string.

var importsGraphics: Bool

A Boolean that controls whether the receiver allows the user to drag image files into it.

Setting the Text Color

var textColor: NSColor?

The color used to draw the receiver’s text.

Auto Layout Sizing

var preferredMaxLayoutWidth: CGFloat

The preferred maximum layout width.

Controlling the Background

var backgroundColor: NSColor?

The color of the background that the receiver’s cell draws behind the text.

var drawsBackground: Bool

A Boolean that controls whether the receiver’s cell draws its background color behind its text

Setting a Border

var isBezeled: Bool

A Boolean that controls whether the receiver draws a bezeled border around its contents.

var bezelStyle: NSTextFieldBezelStyle

The receiver’s bezel style.

var isBordered: Bool

A Boolean that controls whether the receiver draws a solid black border around its contents.

Selecting the Text

func selectText(Any?)

Ends editing and selects the entire contents of the receiver if it’s selectable.

Working with the Responder Chain

var acceptsFirstResponder: Bool

A Boolean value indicating whether the receiver is editable.

Setting the Delegate

var delegate: NSTextFieldDelegate?

The receiver’s delegate.

NSText Delegate Method Implementations

func textShouldBeginEditing(NSText)

Requests permission to begin editing a text object.

func textDidBeginEditing(Notification)

Posts a notification that the text is about to begin editing to the default notification center.

func textDidChange(Notification)

Posts a notification that the text has changed and forwards this message to the receiver’s cell if it responds.

func textShouldEndEditing(NSText)

Performs validation on the receiver’s new value.

func textDidEndEditing(Notification)

Handles an end of editing.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software