Class

NSControl

NSControl is an abstract superclass that provides three fundamental features for implementing user interface devices: drawing devices on the screen, responding to user events, and sending action messages. It works closely with the NSCell class.

Overview

About Delegate Methods

The NSControl class provides several delegate methods for its subclasses that allow text editing, such as NSTextField and NSMatrix. These include: controlTextDidBeginEditing(_:), controlTextDidChange(_:), and controlTextDidEndEditing(_:).

Note that although NSControl defines delegate methods, it does not itself have a delegate. Any subclass that uses these methods must have a delegate and the methods to get and set it. In addition, a formal delegate protocol NSControlTextEditingDelegate also defines delegate methods used by control delegates.

Symbols

Initializing an NSControl

init(frame: NSRect)

Returns an NSControl object initialized with the specified frame rectangle.

Accessing the Control’s Cell

class func cellClass()

Returns the type of cell used by the receiver.

var cell: NSCell?

The receiver’s cell object.

Enabling and Disabling the Control

var isEnabled: Bool

A Boolean value that indicates whether the receiver reacts to mouse events.

Identifying the Selected Cell

func selectedCell()

Returns the receiver’s selected cell.

func selectedTag()

Returns the tag of the receiver’s selected cell.

Accessing the Control’s Value

var doubleValue: Double

The value of the receiver’s cell as a double-precision floating-point number.

var floatValue: Float

The value of the receiver’s cell as a single-precision floating-point number.

var intValue: Int32

The value of the receiver’s cell as an integer.

var integerValue: Int

The value of the receiver’s cell as an NSInteger value.

var objectValue: Any?

The value of the receiver’s cell as an Objective-C object.

var stringValue: String

The value of the receiver’s cell as an NSString object.

func setNeedsDisplay()

Marks the receiver as needing redisplay (assuming automatic display is enabled).

var attributedStringValue: NSAttributedString

The value of the receiver’s cell as an attributed string.

Interacting with Other Controls

func takeDoubleValueFrom(Any?)

Sets the value of the receiver’s cell to a double-precision floating-point value obtained from the specified object.

func takeFloatValueFrom(Any?)

Sets the value of the receiver’s cell to a single-precision floating-point value obtained from the specified object.

func takeIntValueFrom(Any?)

Sets the value of the receiver’s cell to an integer value obtained from the specified object.

func takeIntegerValueFrom(Any?)

Sets the value of the receiver’s cell to an NSInteger value obtained from the specified object.

func takeObjectValueFrom(Any?)

Sets the value of the receiver’s cell to the object value obtained from the specified object.

func takeStringValueFrom(Any?)

Sets the value of the receiver’s cell to the string value obtained from the specified object.

Formatting Text

var alignment: NSTextAlignment

The alignment mode of the text in the receiver’s cell.

var font: NSFont?

The font used to draw text in the receiver’s cell.

var lineBreakMode: NSLineBreakMode

The line break mode to use for text in the control’s cell.

var usesSingleLineMode: Bool

A Boolean value that indicates whether the text in the control’s cell uses single line mode.

var formatter: Formatter?

The receiver’s formatter.

var baseWritingDirection: NSWritingDirection

The initial writing direction used to determine the actual writing direction for text.

Managing Expansion Tool Tips

func draw(withExpansionFrame: NSRect, in: NSView)

Performs custom expansion tool tip drawing.

var allowsExpansionToolTips: Bool

A Boolean value that indicates whether expansion tool tips are shown when the control is hovered over.

func expansionFrame(withFrame: NSRect)

The frame in which a tool tip can be displayed, if needed.

Managing the Field Editor

func abortEditing()

Terminates the current editing operation and discards any edited text.

func currentEditor()

Returns the current field editor for the control.

func validateEditing()

Validates changes to any user-typed text.

func edit(withFrame: NSRect, editor: NSText, delegate: Any?, event: NSEvent)

Begins editing of the receiver’s text using the specified field editor.

func endEditing(NSText)

Ends the editing of text in the receiver using the specified field editor.

func select(withFrame: NSRect, editor: NSText, delegate: Any?, start: Int, length: Int)

Selects the specified text range in the receiver's field editor.

Resizing the Control

var controlSize: NSControlSize

The size of the control.

func sizeThatFits(NSSize)

Asks the control to calculate and return the size that best fits the specified size.

func calcSize()

Recomputes any internal sizing information for the receiver, if necessary.

func sizeToFit()

Resizes the receiver’s frame so that it is the minimum size needed to contain its cell.

Displaying a Cell

var isHighlighted: Bool

A Boolean value that indicates whether the cell is highlighted.

func selectCell(NSCell)

Selects the specified cell and redraws the control as needed.

func drawCell(NSCell)

Draws the specified cell, as long as it belongs to the receiver.

func drawCellInside(NSCell)

Draws the inside of the receiver’s cell (the area within the bezel or border)

func updateCell(NSCell)

Marks the specified cell as in need of redrawing.

func updateCellInside(NSCell)

Marks the inside of the specified cell as in need of redrawing.

Implementing the Target/Action Mechanism

var action: Selector?

The default action-message selector associated with the control.

var target: AnyObject?

The target object that receives action messages from the cell.

var isContinuous: Bool

A Boolean value indicating whether the receiver’s cell sends its action message continuously to its target during mouse tracking.

func sendAction(Selector?, to: Any?)

Causes the specified action to be sent the target.

func sendAction(on: NSEventMask)

Sets the conditions on which the receiver sends action messages to its target.

Accessing Tags

var tag: Int

The tag identifying the receiver (not the tag of the receiver’s cell).

Activating from the Keyboard

func performClick(Any?)

Simulates a single mouse click on the receiver.

var refusesFirstResponder: Bool

A Boolean value indicating whether the receiver refuses the first responder role.

Tracking the Mouse

func mouseDown(with: NSEvent)

Informs the receiver that the user has pressed the left mouse button.

var ignoresMultiClick: Bool

A Boolean value indicating whether the receiver ignores multiple clicks made in rapid succession.

Control Editing Notifications

func controlTextDidBeginEditing(Notification)

Sent when a control with editable text begins an editing session.

func controlTextDidChange(Notification)

Sent when the text in the receiving control changes.

func controlTextDidEndEditing(Notification)

Sent when a control with editable text ends an editing session.

Supporting Constraint-Based Layout

func invalidateIntrinsicContentSize(for: NSCell)

Notifies the control that the intrinsic content size for its cell is no longer valid.

Notifications

An NSControl object posts the following notifications to interested observers and its delegate. Note that although the NSControl class defines delegate methods, it does not itself have a delegate. Any subclass that uses these methods must have a delegate and the methods to get and set it.

static let NSControlTextDidBeginEditing: NSNotification.Name

Sent when a control with editable cells begins an edit session.

static let NSControlTextDidChange: NSNotification.Name

Sent when the text in the receiving control changes.

static let NSControlTextDidEndEditing: NSNotification.Name

Sent when a control with editable cells ends an editing session.

Relationships

Inherits From