The NSText​Container class defines a region where text is laid out. An NSLayout​Manager uses NSText​Container to determine where to break lines, lay out portions of text, and so on. An NSText​Container object normally defines rectangular regions, but you can define exclusion paths inside the text container to create regions where text does not flow. You can also subclass to create text containers with nonrectangular regions, such as circular regions, regions with holes in them, or regions that flow alongside graphics.


Instances of the NSText​Container, NSLayout​Manager, and NSText​Storage classes can be accessed from threads other than the main thread as long as the app guarantees access from only one thread at a time.


Creating a Text Container

init(size:​ CGSize)

Initializes a text container with a specified bounding rectangle.

Managing Text Components

var layout​Manager:​ NSLayout​Manager?

The receiver’s layout manager.

func replace​Layout​Manager(NSLayout​Manager)

Replaces the layout manager for the group of text system objects containing the receiver.

var text​View:​ NSText​View?

The receiver’s text view.

Defining the Container Shape

var size:​ CGSize

The size of the text container’s bounding rectangle.

var exclusion​Paths:​ [UIBezier​Path]

An array of path objects representing the regions where text is not displayed in the text container.

var line​Break​Mode:​ NSLine​Break​Mode

The behavior of the last line inside the text container.

var container​Size:​ NSSize

The size of the receiver’s bounding rectangle.

var width​Tracks​Text​View:​ Bool

A Boolean that controls whether the receiver adjusts the width of its bounding rectangle when its text view is resized.

var height​Tracks​Text​View:​ Bool

A Boolean that controls whether the receiver adjusts the height of its bounding rectangle when its text view is resized.

Constraining Text Layout

var maximum​Number​Of​Lines:​ Int

The maximum number of lines that can be stored in the text container.

var line​Fragment​Padding:​ CGFloat

The amount by which text is inset within line fragment rectangles.

var is​Simple​Rectangular​Text​Container:​ Bool

A boolean that indicates whether the receiver’s region is a rectangle with no holes or gaps and whose edges are parallel to the text view's coordinate system axes.


init(container​Size:​ NSSize)

Initializes a text container with a specified bounding rectangle.

func line​Fragment​Rect(for​Proposed​Rect:​ NSRect, sweep​Direction:​ NSLine​Sweep​Direction, movement​Direction:​ NSLine​Movement​Direction, remaining:​ NSRect​Pointer?)

Overridden by subclasses to calculate and return the longest rectangle available in the proposed rectangle for displaying text, or NSZero​Rect if there is none according to the receiver’s region definition.

func contains(NSPoint)

Overridden by subclasses to return whether a point lies within the receiver’s region or on the region’s edge—not simply within its bounding rectangle.




These constants describe the progression of text on a page. The typesetter decides which way text is supposed to flow and passes these values as arguments to the text container, which uses them to calculate the next line rectangle.


Line movement is the direction in which lines move. See Text System Storage Layer Overview.