Mac Developer Library

Developer

AppKit Framework Reference NSTextField Class Reference

Options
Deployment Target:

On This Page
Language:

NSTextField

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.

Import Statement


Swift

import AppKit

Objective-C

@import AppKit;

Availability


Available in OS X v10.0 and later.
  • Controls whether the user can edit the receiver’s text.

    Declaration

    Swift

    var editable: Bool

    Objective-C

    @property(getter=isEditable) BOOL editable

    Parameters

    flag

    If YEStrue, then the user is allowed to both select and edit text. If flag is NOfalse, then the user isn’t permitted to edit text and the ability to select the receiver’s content is dependent on the value of isSelectable.

    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.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

    See Also

    – isEditable

  • isEditable - isEditable Available in OS X v10.0 through OS X v10.9

    Returns a Boolean value indicating whether the user is allowed to select and edit the receiver's text.

    Declaration

    Objective-C

    - (BOOL)isEditable

    Return Value

    YEStrue if the user is allowed to select and edit the receiver’s text, NOfalse if the user isn’t allowed to edit it (though the user may be able to select it).

    Import Statement

    Objective-C

    @import AppKit;

    Availability

    Available in OS X v10.0 through OS X v10.9.

  • Sets whether the receiver is selectable (but not editable).

    Declaration

    Swift

    var selectable: Bool

    Objective-C

    @property(getter=isSelectable) BOOL selectable

    Parameters

    flag

    If YEStrue, the receiver is made selectable but not editable (use setEditable: to make text both selectable and editable). If NOfalse, the text is neither editable nor selectable.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • isSelectable - isSelectable Available in OS X v10.0 through OS X v10.9

    Returns a Boolean value indicating whether the user is allowed to select the receiver’s text.

    Declaration

    Objective-C

    - (BOOL)isSelectable

    Return Value

    YEStrue if the user is allowed to select the receiver’s text; otherwise NOfalse.

    Discussion

    Selectable text isn’t necessarily editable; use isEditable to determine if the text is editable.

    Import Statement

    Objective-C

    @import AppKit;

    Availability

    Available in OS X v10.0 through OS X v10.9.

  • Controls whether the receiver allows the user to change font attributes of the receiver’s string.

    Declaration

    Swift

    var allowsEditingTextAttributes: Bool

    Objective-C

    @property BOOL allowsEditingTextAttributes

    Parameters

    flag

    If YEStrue, 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 is NOfalse and 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.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Returns a Boolean value indicating whether the user is allowed to change font attributes of the receiver's text.

    Declaration

    Swift

    var allowsEditingTextAttributes: Bool

    Objective-C

    @property BOOL allowsEditingTextAttributes

    Return Value

    YEStrue if the receiver allows the user to change font attributes of the receiver’s text, otherwise NOfalse. You can change text attributes programmatically regardless of this setting.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Controls whether the receiver allows the user to drag image files into it.

    Declaration

    Swift

    var importsGraphics: Bool

    Objective-C

    @property BOOL importsGraphics

    Parameters

    flag

    If YEStrue, the receiver accepts dragged images; if NOfalse, it doesn’t. You can add images programmatically regardless of this setting.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Returns a Boolean value indicating whether the receiver allows the user to drag image files into it.

    Declaration

    Swift

    var importsGraphics: Bool

    Objective-C

    @property BOOL importsGraphics

    Return Value

    YEStrue if the receiver allows the user to drag image files into it, otherwise NOfalse. You can add images programmatically regardless of this setting.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Sets the color used to draw the receiver’s text.

    Declaration

    Swift

    @NSCopying var textColor: NSColor?

    Objective-C

    @property(copy) NSColor *textColor

    Parameters

    aColor

    The color used to draw text.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Returns the color used to draw the receiver’s text.

    Declaration

    Swift

    @NSCopying var textColor: NSColor?

    Objective-C

    @property(copy) NSColor *textColor

    Return Value

    The color used to draw text.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Returns the preferred maximum layout width.

    Declaration

    Swift

    var preferredMaxLayoutWidth: CGFloat

    Objective-C

    @property CGFloat preferredMaxLayoutWidth

    Return Value

    The preferred maximum layout width.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • Sets the preferred maximum width of the text field.

    Declaration

    Swift

    var preferredMaxLayoutWidth: CGFloat

    Objective-C

    @property CGFloat preferredMaxLayoutWidth

    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.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

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

    Declaration

    Swift

    @NSCopying var backgroundColor: NSColor?

    Objective-C

    @property(copy) NSColor *backgroundColor

    Parameters

    aColor

    The color used to draw the background.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

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

    Declaration

    Swift

    @NSCopying var backgroundColor: NSColor?

    Objective-C

    @property(copy) NSColor *backgroundColor

    Return Value

    The color used to draw the background.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Controls whether the receiver’s cell draws its background color behind its text.

    Declaration

    Swift

    var drawsBackground: Bool

    Objective-C

    @property BOOL drawsBackground

    Parameters

    flag

    If YEStrue, the receiver’s cell draws its background; if NOfalse, it draws nothing behind its text.

    Discussion

    In order to prevent inconsistent rendering, background color rendering is disabled for rounded-bezel text fields.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Returns a Boolean value indicating whether the receiver's cell draws its background color.

    Declaration

    Swift

    var drawsBackground: Bool

    Objective-C

    @property BOOL drawsBackground

    Return Value

    YEStrue if the receiver’s cell draws its background color behind its text, NOfalse if it draws no background.

    Discussion

    In order to prevent inconsistent rendering, background color rendering is disabled for rounded-bezel text fields.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Controls whether the receiver draws a bezeled border around its contents.

    Declaration

    Swift

    var bezeled: Bool

    Objective-C

    @property(getter=isBezeled) BOOL bezeled

    Parameters

    flag

    If YEStrue, it draws a bezeled border and invokes setDrawsBackground: with an argument of NOfalse; if NOfalse, the receiver does not draw a border.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • isBezeled - isBezeled Available in OS X v10.0 through OS X v10.9

    Returns a Boolean value indicating whether the receiver draws a bezeled frame.

    Declaration

    Objective-C

    - (BOOL)isBezeled

    Return Value

    YEStrue if the receiver draws a bezeled frame around its contents; otherwise, NOfalse.

    Import Statement

    Objective-C

    @import AppKit;

    Availability

    Available in OS X v10.0 through OS X v10.9.

  • Sets the receiver’s bezel style.

    Declaration

    Swift

    var bezelStyle: NSTextFieldBezelStyle

    Objective-C

    @property NSTextFieldBezelStyle bezelStyle

    Parameters

    style

    A constant indicating the bezel style. Possible values for style are described in NSTextFieldBezelStyle. You must have already sent the receiver setBezeled: with an argument of YEStrue

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.2 and later.

    See Also

    – bezelStyle

  • Returns the receiver’s bezel style.

    Declaration

    Swift

    var bezelStyle: NSTextFieldBezelStyle

    Objective-C

    @property NSTextFieldBezelStyle bezelStyle

    Return Value

    A constant indicating the bezel style. Possible values described in NSTextFieldBezelStyle.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.2 and later.

  • Controls whether the receiver draws a solid black border around its contents.

    Declaration

    Swift

    var bordered: Bool

    Objective-C

    @property(getter=isBordered) BOOL bordered

    Parameters

    flag

    If YEStrue, the receiver draws a border; if NOfalse, it draws no border.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • isBordered - isBordered Available in OS X v10.0 through OS X v10.9

    Returns a Boolean value indicating whether the receiver draws a black border around its contents.

    Declaration

    Objective-C

    - (BOOL)isBordered

    Return Value

    YEStrue if the receiver draws a solid black border around its contents; otherwise NOfalse.

    Import Statement

    Objective-C

    @import AppKit;

    Availability

    Available in OS X v10.0 through OS X v10.9.

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

    Declaration

    Swift

    func selectText(_ sender: AnyObject?)

    Objective-C

    - (void)selectText:(id)sender

    Parameters

    sender

    The sender of the message.

    Discussion

    If the receiver isn’t in some window’s view hierarchy, this method has no effect.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Returns a Boolean value indicating whether the receiver is editable.

    Declaration

    Swift

    var acceptsFirstResponder: Bool { get }

    Objective-C

    @property(readonly) BOOL acceptsFirstResponder

    Return Value

    YEStrue if the receiver is editable, NOfalse otherwise.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Sets the receiver’s string value, using the embedded character as the keyboard mnemonic.

    Declaration

    Objective-C

    - (void)setTitleWithMnemonic:(NSString *)aString

    Parameters

    aString

    The string to set as the title. The first character preceded by an ampersand (‘&’) is used as the mnemonic (the first ampersand character is stripped out).

    Discussion

    Mnemonics are not supported in OS X.

    Import Statement

    Objective-C

    @import AppKit;

    Availability

    Available in OS X v10.0 and later.

    Deprecated in OS X v10.8.

  • Sets the receiver’s delegate.

    Declaration

    Swift

    unowned(unsafe) var delegate: NSTextFieldDelegate?

    Objective-C

    @property(assign) id<NSTextFieldDelegate> delegate

    Parameters

    anObject

    The object that acts as the receiver's delegate.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

    See Also

    – delegate

  • Returns the receiver’s delegate.

    Declaration

    Swift

    unowned(unsafe) var delegate: NSTextFieldDelegate?

    Objective-C

    @property(assign) id<NSTextFieldDelegate> delegate

    Return Value

    The object that acts as the receiver's delegate.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.