Adding Multiple NSTextContainers to an NSLayoutManager

1.3k Views Asked by At

I notice that you can add multiple text containers to a layout manager in Objective-C using the addTextContainer:(NSTextContainer *)textContainer selector. Using TextKit, I've only ever had to add one text container, and when messing around with multiple text containers, all of the textviews just get filled with the same text from the text storage linked to the layout manager. I'm simply curious, but when would I ever need to add multiple text containers to one layout manager?

2

There are 2 best solutions below

0
On

... the NSTextStorage object’s data and lay them out within the area defined by the NSTextContainer object.

This configuration is limited by having only one NSTextContainer-NSTextView pair. In such an arrangement, the text flows uninterrupted within the area defined by the NSTextContainer.

Page breaks, multicolumn layout, and more complex layouts can’t be accommodated by this arrangement.

By using multiple NSTextContainer-NSTextView pairs, more complex layout arrangements are possible. For example, to support page breaks, an application can configure the text objects as shown in Figure 3-4.

Common Configurations

https://developer.apple.com/library/content/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextSystemArchitecture/ArchitectureOverview.html

0
On

One NSLayoutManger to Multiple Text Containers when you need the text within the associated NSTextStorage to span over multiple pages (normally would each be an NSTextView) as in a word processor spreading its text over multiple pages.

The NSLayoutManager is clever enough to split the text in NSTextStorage into each NSTextContainer.