NSTextField Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSTextField.h |
Overview
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: setStringValue:, setDoubleValue:, etc.. There are corresponding methods to retrieve values.
Tasks
Controlling Selection and Editing
Controlling Rich Text Behavior
-
– setAllowsEditingTextAttributes: -
– allowsEditingTextAttributes -
– setImportsGraphics: -
– importsGraphics
Setting the Text Color
Autolayout Sizing
Controlling the Background
Setting a Border
Selecting the Text
Working with the Responder Chain
-
– acceptsFirstResponder -
– nextTextAvailable in OS X v10.0 through OS X v10.1 -
– previousTextAvailable in OS X v10.0 through OS X v10.1 -
– setNextText:Available in OS X v10.0 through OS X v10.1 -
– setPreviousText:Available in OS X v10.0 through OS X v10.1
Using Keyboard Interface Control
-
– setTitleWithMnemonic:Deprecated in OS X v10.8
Setting the Delegate
NSText Delegate Method Implementations
Instance Methods
acceptsFirstResponder
Returns a Boolean value indicating whether the receiver is editable.
Availability
- Available in OS X v10.0 and later.
Declared In
NSTextField.hallowsEditingTextAttributes
Returns a Boolean value indicating whether the user is allowed to change font attributes of the receiver's text.
Return Value
YES if the receiver allows the user to change font attributes of the receiver’s text, otherwise NO. You can change text attributes programmatically regardless of this setting.
Availability
- Available in OS X v10.0 and later.
Declared In
NSTextField.hbackgroundColor
Returns the color of the background that the receiver’s cell draws behind the text.
Return Value
The color used to draw the background.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hbezelStyle
Returns the receiver’s bezel style.
Return Value
A constant indicating the bezel style. Possible values described in NSTextFieldBezelStyle.
Availability
- Available in OS X v10.2 and later.
See Also
Declared In
NSTextField.hdelegate
Returns the receiver’s delegate.
Return Value
The object that acts as the receiver's delegate.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hdrawsBackground
Returns a Boolean value indicating whether the receiver's cell draws its background color.
Return Value
YES if the receiver’s cell draws its background color behind its text, NO if it draws no background.
Discussion
In order to prevent inconsistent rendering, background color rendering is disabled for rounded-bezel text fields.
Availability
- Available in OS X v10.0 and later.
See Also
-
– backgroundColor -
drawsBackground(NSTextFieldCell) -
– setDrawsBackground:
Declared In
NSTextField.himportsGraphics
Returns a Boolean value indicating whether the receiver allows the user to drag image files into it.
Return Value
YES if the receiver allows the user to drag image files into it, otherwise NO. You can add images programmatically regardless of this setting.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hisBezeled
Returns a Boolean value indicating whether the receiver draws a bezeled frame.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hisBordered
Returns a Boolean value indicating whether the receiver draws a black border around its contents.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hisEditable
Returns a Boolean value indicating whether the user is allowed to select and edit the receiver's text.
Return Value
YES if the user is allowed to select and edit the receiver’s text, NO if the user isn’t allowed to edit it (though the user may be able to select it).
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hisSelectable
Returns a Boolean value indicating whether the user is allowed to select the receiver’s text.
Discussion
Selectable text isn’t necessarily editable; use isEditable to determine if the text is editable.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hpreferredMaxLayoutWidth
Returns the preferred maximum layout width.
Return Value
The preferred maximum layout width.
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSTextField.hselectText:
Ends editing and selects the entire contents of the receiver if it’s selectable.
Parameters
- sender
The sender of the message.
Discussion
If the receiver isn’t in some window’s view hierarchy, this method has no effect.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetAllowsEditingTextAttributes:
Controls whether the receiver allows the user to change font attributes of the receiver’s string.
Parameters
- flag
If
YES, and the text value is an attributed string, it is displayed using the attributed string’s visual settings, which can be modified in the font panel.; if flag isNOand the text is an attributed string. the string attributes (font, color, etc.) are ignored and the string is displayed based on the text field’s settings. Setting the attributed string’s attributes are ignored when displaying the string and when the text field is editing.
Availability
- Available in OS X v10.0 and later.
Declared In
NSTextField.hsetBackgroundColor:
Sets the color of the background that the receiver’s cell draws behind the text.
Parameters
- aColor
The color used to draw the background.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetBezeled:
Controls whether the receiver draws a bezeled border around its contents.
Parameters
- flag
If
YES, it draws a bezeled border and invokessetDrawsBackground:with an argument ofNO; ifNO, the receiver does not draw a border.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetBezelStyle:
Sets the receiver’s bezel style.
Parameters
- style
A constant indicating the bezel style. Possible values for style are described in
NSTextFieldBezelStyle. You must have already sent the receiversetBezeled:with an argument ofYES
Availability
- Available in OS X v10.2 and later.
See Also
Declared In
NSTextField.hsetBordered:
Controls whether the receiver draws a solid black border around its contents.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetDelegate:
Sets the receiver’s delegate.
Parameters
- anObject
The object that acts as the receiver's delegate.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetDrawsBackground:
Controls whether the receiver’s cell draws its background color behind its text.
Parameters
Discussion
In order to prevent inconsistent rendering, background color rendering is disabled for rounded-bezel text fields.
Availability
- Available in OS X v10.0 and later.
See Also
-
– setBackgroundColor: -
setDrawsBackground:(NSTextFieldCell) -
– drawsBackground
Declared In
NSTextField.hsetEditable:
Controls whether the user can edit the receiver’s text.
Parameters
- flag
If
YES, then the user is allowed to both select and edit text. If flag isNO, then the user isn’t permitted to edit text and the ability to select the receiver’s content is dependent on the value ofisSelectable.
Discussion
For example, if an NSTextField object is selectable but not editable, then made editable for a time, then made not editable, it remains selectable. To guarantee that text is neither editable or selectable, use setSelectable: to disable text selection.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetImportsGraphics:
Controls whether the receiver allows the user to drag image files into it.
Parameters
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetPreferredMaxLayoutWidth:
Sets the preferred maximum width of the text field.
Parameters
- width
The preferred maximum width.
Discussion
The value determines the maximum width of the intrinsicContentSize. If the text field wraps, the intrinsic height will be large enough to show the entire text contents at that width.
The default is 0, which implies no maximum preferred width.
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSTextField.hsetSelectable:
Sets whether the receiver is selectable (but not editable).
Parameters
- flag
If
YES, the receiver is made selectable but not editable (usesetEditable:to make text both selectable and editable). IfNO, the text is neither editable nor selectable.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.hsetTextColor:
Sets the color used to draw the receiver’s text.
Parameters
- aColor
The color used to draw text.
Availability
- Available in OS X v10.0 and later.
See Also
-
– setBackgroundColor: -
setTextColor:(NSTextFieldCell) -
– textColor
Declared In
NSTextField.htextColor
Returns the color used to draw the receiver’s text.
Return Value
The color used to draw text.
Availability
- Available in OS X v10.0 and later.
See Also
-
– backgroundColor -
textColor(NSTextFieldCell) -
– setTextColor:
Declared In
NSTextField.htextDidBeginEditing:
Posts a notification that the text is about to begin editing to the default notification center.
Parameters
- aNotification
The
NSControlTextDidBeginEditingNotificationnotification to post.
Discussion
This action causes the receiver’s delegate to receive a controlTextDidBeginEditing: message. See the NSControl class specification for more information on the text delegate method.
Availability
- Available in OS X v10.0 and later.
Declared In
NSTextField.htextDidChange:
Posts a notification that the text has changed and forwards this message to the receiver’s cell if it responds.
Parameters
- aNotification
The
NSControlTextDidChangeNotificationnotification that is posted to the default notification center.
Discussion
This method causes the receiver’s delegate to receive a controlTextDidChange: message. See the NSControl class specification for more information on the text delegate method.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSTextField.htextDidEndEditing:
Handles an end of editing.
Parameters
- aNotification
The notification that editing has ended.
Discussion
After validating the new value, posts an NSControlTextDidEndEditingNotification to the default notification center. This posting causes the receiver’s delegate to receive a controlTextDidEndEditing: message. After this message, sends endEditing: to the receiver’ cell and handles the key that caused editing to end:
If the user ended editing by pressing Return, this method tries to send the receiver’s action to its target; if unsuccessful, it sends
performKeyEquivalent:to itsNSView(for example, to handle the default button on a panel); if that also fails, the receiver simply selects its text.If the user ended editing by pressing Tab or Shift-Tab, the receiver tries to have its
NSWindowobject select its next or previous key view, using theNSWindowmethodselectKeyViewFollowingView:orselectKeyViewPrecedingView:. If unsuccessful in doing this, the receiver simply selects its text.
See the NSControl class specification for more information on the text delegate method.
Availability
- Available in OS X v10.0 and later.
Declared In
NSTextField.htextShouldBeginEditing:
Requests permission to begin editing a text object.
Parameters
- textObject
The object to begin editing.
Return Value
YES if editing should be allowed to occur, NO otherwise.
Discussion
If the receiver isn’t editable, returns NO immediately. If it is editable and its delegate responds to control:textShouldBeginEditing:, this invokes that method and returns the result. Otherwise it simply returns YES to allow editing to occur. See the NSControl class specification for more information on the text delegate method.
Availability
- Available in OS X v10.0 and later.
Declared In
NSTextField.htextShouldEndEditing:
Performs validation on the receiver’s new value.
Parameters
- textObject
The text object requesting permission to end editing.
Return Value
YES if the new value is valid; otherwise NO.
Discussion
This method validates the receiver's new value using the NSCell method isEntryAcceptable:. If the new value is valid and the delegate responds to control:textShouldEndEditing:, invokes that method and returns the result, in addition beeping if the delegate returns NO. See the NSControl class specification for more information on the text delegate method.
Availability
- Available in OS X v10.0 and later.
Declared In
NSTextField.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)