<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSBox",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSBox"
  },
  "title" : "NSBox"
}
-->

# NSBox

A stylized rectangular box with an optional title.

```
class NSBox
```

## Overview

Use box objects to visually group the contents of your window. For example, you might use boxes to group related views. Use an [`NSBox`](/documentation/AppKit/NSBox) object to configure the appearance of the box.

### 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(_:)`](/documentation/AppKit/NSView/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(_:)`](/documentation/AppKit/NSView/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`](/documentation/AppKit/NSBox/contentView), [`sizeToFit()`](/documentation/AppKit/NSBox/sizeToFit()), or [`addSubview(_:)`](/documentation/AppKit/NSView/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.

## Topics

### Configuring Boxes

[`borderRect`](/documentation/AppKit/NSBox/borderRect)

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

[`boxType`](/documentation/AppKit/NSBox/boxType-swift.property)

The receiver’s box type.

[`borderType`](/documentation/AppKit/NSBox/borderType)

The receiver’s border type.

[`transparent`](/documentation/AppKit/NSBox/isTransparent)

A Boolean value that indicates whether the receiver is transparent.

[`title`](/documentation/AppKit/NSBox/title)

The receiver’s title.

[`titleFont`](/documentation/AppKit/NSBox/titleFont)

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

[`titlePosition`](/documentation/AppKit/NSBox/titlePosition-swift.property)

A constant representing the title position.

[`-  setTitleWithMnemonic:`](/documentation/AppKit/NSBox/setTitleWithMnemonic:)

Sets the title of the receiver with a character denoted as an access key.

[`titleCell`](/documentation/AppKit/NSBox/titleCell)

The cell used to display the receiver’s title.

[`titleRect`](/documentation/AppKit/NSBox/titleRect)

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

### Customizing

[`borderColor`](/documentation/AppKit/NSBox/borderColor)

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

[`borderWidth`](/documentation/AppKit/NSBox/borderWidth)

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

[`cornerRadius`](/documentation/AppKit/NSBox/cornerRadius)

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

[`fillColor`](/documentation/AppKit/NSBox/fillColor)

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

### Managing Content

[`contentView`](/documentation/AppKit/NSBox/contentView)

The receiver’s content view.

[`contentViewMargins`](/documentation/AppKit/NSBox/contentViewMargins)

The distances between the border and the content view.

### Sizing

[`-  setFrameFromContentFrame:`](/documentation/AppKit/NSBox/setFrameFromContentFrame(_:))

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

[`-  sizeToFit`](/documentation/AppKit/NSBox/sizeToFit())

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

### Constants

[`TitlePosition`](/documentation/AppKit/NSBox/TitlePosition-swift.enum)

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

[`BoxType`](/documentation/AppKit/NSBox/BoxType-swift.enum)

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

## Relationships

### Conforms To

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSDraggingDestination`](/documentation/AppKit/NSDraggingDestination)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSAccessibilityElementProtocol`](/documentation/AppKit/NSAccessibilityElementProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`Sendable`](/documentation/Swift/Sendable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSTouchBarProvider`](/documentation/AppKit/NSTouchBarProvider)

[`NSUserInterfaceItemIdentification`](/documentation/AppKit/NSUserInterfaceItemIdentification)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`NSStandardKeyBindingResponding`](/documentation/AppKit/NSStandardKeyBindingResponding)

[`NSAppearanceCustomization`](/documentation/AppKit/NSAppearanceCustomization)

[`NSAccessibilityProtocol`](/documentation/AppKit/NSAccessibilityProtocol)

[`NSAnimatablePropertyContainer`](/documentation/AppKit/NSAnimatablePropertyContainer)

[`NSUserActivityRestoring`](/documentation/AppKit/NSUserActivityRestoring)

### Inherits From

[`NSView`](/documentation/AppKit/NSView)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)