You can set an NSTextContainer to track the size of its NSTextView and adjust its own size to match whenever the NSTextView size changes. The setHeightTracksTextView:
and setWidthTracksTextView:
methods allow you to control this tracking for either dimension.
When an NSTextContainer adjusts its size to match that of its NSTextView, it takes into account the inset specified by the NSTextView so that the bounding rectangle is inset from every edge possible. In other words, an NSTextContainer that tracks the size of its NSTextView is always smaller than the NSTextView (in the appropriate dimension) by twice the inset. Suppose an NSTextContainer is set to track width and its NSTextView gives it an inset of (10,10). Now, if the NSTextView’s width is changed to 138, the NSTextContainer’s top-left corner is set to lie at (10,10) and its width is set to 118, so that its right edge is 10 points from the NSTextView’s right edge. Its height remains the same.
Whether it tracks the size of its NSTextView or not, an NSTextContainer doesn’t grow or shrink as text is added or deleted; instead, the NSLayoutManager resizes the NSTextView based on the portion of the NSTextContainer actually filled with text. To allow an NSTextView to be resized in this manner, use NSTextView’s setVerticallyResizable:
or setHorizontallyResizable:
methods (which are inherited from NSText) as needed, set the text container not to track the size of its text view, and set the text container’s size in the appropriate dimension large enough to accommodate a great amount of text—for example, 10,000,000 points (this incurs no cost whatever in processing or storage).
Note that an NSTextView can be resized based on its NSTextContainer, and an NSTextContainer can resize itself based on its NSTextView. If you set both objects up to resize automatically in the same dimension, your application can get trapped in an infinite loop. When text is added to the NSTextContainer, the NSTextView is resized to fit the area actually used for text; this causes the NSTextContainer to resize itself and relay its text, which causes the NSTextView to resize itself again, and so on ad infinitum. Each type of size tracking has its proper uses; be sure to use only one for either dimension.
Last updated: 2005-08-11