A region where text is laid out.


class NSTextContainer : NSObject


An NSLayoutManager uses NSTextContainer to determine where to break lines, lay out portions of text, and so on. An NSTextContainer object typically 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 NSTextContainer, NSLayoutManager, and NSTextStorage 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 layoutManager: NSLayoutManager?

The receiver’s layout manager.

func replaceLayoutManager(NSLayoutManager)

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

var textView: NSTextView?

The receiver’s text view.

Defining the Container Shape

var size: CGSize

The size of the text container’s bounding rectangle.

var exclusionPaths: [UIBezierPath]

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

var lineBreakMode: NSLineBreakMode

The behavior of the last line inside the text container.

var widthTracksTextView: Bool

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

var heightTracksTextView: 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 maximumNumberOfLines: Int

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

var lineFragmentPadding: CGFloat

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

var isSimpleRectangularTextContainer: 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(containerSize: NSSize)

Initializes a text container with a specified bounding rectangle.

func lineFragmentRect(forProposedRect: NSRect, sweepDirection: NSLineSweepDirection, movementDirection: NSLineMovementDirection, remaining: NSRectPointer?) -> NSRect

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

func contains(NSPoint) -> Bool

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.

var containerSize: NSSize

The size of the receiver’s bounding rectangle.


See Also


class NSTextStorage

The fundamental storage mechanism of TextKit.

protocol NSTextStorageDelegate

A set of optional methods implemented by delegates of NSTextStorage objects to handle text-edit processing.

class NSAttributedString

A string that has associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.