The NSBox class implements simple views that can title themselves and draw a border around their content. These objects are known as boxes. You can use box to group, visually, some number of other views.


Subclassing Notes

An NSBox object is a view that draws a line around its rectangular bounds and that displays a title on or near the line (or might display neither line nor title). You can adjust the style of the line (bezel, grooved, or plain) as well as the placement and font of the title. An NSBox also has a content view to which other views can be added; it thus offers a way for an application to group related views. You could create a custom subclass of NSBox that alters or augments its appearance or that modifies its grouping behavior. For example, you might add color to the lines or background, add a new line style, or have the views in the group automatically snap to an invisible grid when added.

Methods to Override

You must override the draw(_:) method (inherited from NSView) if you want to customize the appearance of your NSBox objects. Depending on the visual effect you’re trying to achieve, you may have to invoke super’s implementation first. For example, if you are compositing a small image in a corner of the box, you would invoke the superclass implementation first. If you’re adding a new style of line, you would provide a way to store a request for this line type (such as a boolean instance variable and related accessor methods). Then, in draw(_:), if a request for this line type exists, you would draw the entire view yourself (that is, without calling super). Otherwise, you would invoke the superclass implementation.

If you wish to change grouping behavior or other behavioral characteristics of the NSBox class, consider overriding contentView, sizeToFit(), or addSubview(_:) (inherited from NSView).

Special Considerations

If you are drawing the custom NSBox entirely by yourself, and you want it to look exactly like the superclass object (except for your changes), it may take some effort and time to get the details right.


Configuring Boxes

var borderRect: NSRect

The rectangle in which the receiver’s border is drawn.

var boxType: NSBoxType

The receiver’s box type.

var borderType: NSBorderType

The receiver’s border type.

var isTransparent: Bool

A Boolean value that indicates whether the receiver is transparent.

var title: String

The receiver’s title.

var titleFont: NSFont

The font object used to draw the receiver’s title.

var titlePosition: NSTitlePosition

A constant representing the title position.

var titleCell: Any

The cell used to display the receiver’s title.

var titleRect: NSRect

The rectangle in which the receiver’s title is drawn.


var borderColor: NSColor

The color of the receiver’s border when the receiver is a custom box with a simple line border.

var borderWidth: CGFloat

The width of the receiver’s border when the receiver is a custom box with a simple line border.

var cornerRadius: CGFloat

The radius of the receiver’s corners when the receiver is a custom box with a simple line border.

var fillColor: NSColor

The color of the receiver’s background when the receiver is a custom box with a simple line border.

Managing Content

var contentView: NSView?

The receiver’s content view.

var contentViewMargins: NSSize

The distances between the border and the content view.


func setFrameFromContentFrame(NSRect)

Places the receiver so its content view lies on the specified frame.

func sizeToFit()

Resizes and moves the receiver’s content view so it just encloses its subviews.



Specify the location of a box’s title with respect to its border.


These constants and data type identifies box types, which, in conjunction with a box's border type, define the appearance of the box.


Inherits From