Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
From the author of The High-Level View

The High-Level View

Most of the time you spend using the text system, you won't need to deal with the low-level NSTypesetter details. Most of the time, you'll only need to use three classes directly:

  • NSTextStorage
  • NSTextView
  • NSTextContainer

NSTextStorage is a subclass of NSMutableAttributedString, which provides some extra features for supporting editing from a user interface, among other things. This is the model object in the model-view-controller (MVC) pattern, and NSTextView is the corresponding view.

If you create an NSTextView in Interface Builder, one of each of these three classes is created. An NSScrollView (and a clip view) are created, as well as a pair of scrollers to control displaying portions of the text view. As with other Cocoa views, the text view doesn't implement scrolling itself; it just provides the ability to draw a subset of itself when required. The scroll and clip views handle displaying a portion of the larger view in response to scrolling. This distinction is very important, because it means that you can use an NSTextView inside some other kind of view to display a block of text. The MultiText example, which I explain in both Cocoa Programming Developer's Handbook and my new Cocoa Programming Fundamentals LiveLessons (it's in the same examples.zip download file as the TextWheel example), shows a simple example, using a set of NSTextViews without scrollers to implement variable-sized text columns in an NSSplitView.

If you're implementing something that needs DTP-like behavior, you can create text views exactly where you need blocks of text, without scroll bars. Each text view has a single associated text container, but you can associate several text containers with a single layout manager, which means that text will automatically flow from the end of one text view to the start of the next.

The Cocoa text system gives you everything you need for laying out text, whether you're just creating simple labels in a GUI or creating a rich desktop publishing application for documents with hundreds of pages. It's one of the most powerful bits of Cocoa, and the fact that most developers use it without ever realizing its power and flexibility is a demonstration of how well it's designed. You're only exposed to the advanced features if you need them.

  • + Share This
  • 🔖 Save To Your Account