Layout Geometry: The NSTextContainer Class

An NSTextContainer object defines the area on a page in which the Cocoa text system lays out text. By default, a text container defines a simple, rectangular area, but you can create subclasses that define areas with any geometrical shape, including regions with holes around which text flows.

NSTextContainer provides one of the four primary text objects in the Cocoa text system. Text containers work with text storage objects, layout managers, and text views to store, lay out, and display attributed text strings. In particular, a text container works directly with a layout manager, which uses an NSTypesetter object to generate line-fragment rectangles in which to place glyphs (character shapes), as described in Line Fragment Generation

When the typesetter generates line fragments, the text container is particularly concerned with the direction in which text layout proceeds. There are two aspects to layout direction: line sweep and line movement. Line sweep is the direction in which the system lays out glyphs within lines. Line movement is the direction in which the system lays out lines upon the page. The typesetter object determines these parameters and passes them as constant values to the text container. Both line sweep and line movement can proceed from left to right, right to left, top to bottom, and bottom to top. In addition, the typesetter can specify no line movement.

The layout manager maintains an array of text containers. It sends a message to its delegate whenever it fills a text container, and the delegate can then add a new text container to be filled. If a text container changes size, or if changes to laid-out text in a container invalidate layout at that point, then the system invalidates the layout in all the subsequent containers in the layout manager’s array.

You can specify that a text container track the size of its text view; that is, if the user resizes the view, the text container resizes itself to match. For more information, see Tracking the Size of a Text View.

NSTextContainer instances have methods for initialization, managing connection to layout managers and text views, getting and setting the container size, generating line fragments, and hit-testing. In addition, NSTextContainer has methods for getting and setting the amount of padding to apply to line fragments. Line fragment padding is extra space included at the ends of line fragments so laid-out glyphs don’t directly touch other elements on the page, such as graphics.