The Storage Layer: The NSTextStorage Class

An NSTextStorage object serves as the character data repository for the Cocoa text system. The format for this data is an attributed string, which is a sequence of characters (in Unicode encoding) and the attributes (such as font, color, and paragraph style) that apply to them. The classes that represent attributed strings are NSAttributedString and NSMutableAttributedString, of which NSTextStorage is a subclass. Conceptually, each character in a block of text has a dictionary of keys and values associated with it. A key names an attribute (such as NSFontAttributeName), and the associated value specifies the characteristics of that attribute (such as Helvetica 12-point). For more information about attributed strings, see Attributed String Programming Guide. Figure 1 illustrates the NSTextStorage class, showing its NSMutableAttributedString component and its additional capabilities.

Figure 1  Capabilities of NSTextStorage
Capabilities of NSTextStorage

The NSTextStorage methods let you operate programmatically on the attributes of the text displayed by the NSTextView object; for example, your code can iterate through the text, tightening or loosening the kerning for all characters of a certain font and size. An NSTextView object enables users to affect character attributes through direct action; for example, the user selects some text and reduces the spacing between characters by choosing the Tighten menu command.