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.
- macOS 10.10+
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 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.