The fundamental storage mechanism of TextKit.


class NSTextStorage : NSMutableAttributedString


NSTextStorage is a semi concrete subclass of NSMutableAttributedString that adds behavior for managing a set of client NSLayoutManager objects. A text storage object notifies its layout managers of changes to its characters or attributes, which lets the layout managers redisplay the text as needed.

A text storage object can be accessed from any thread of your app, but your app must guarantee access from only one thread at a time.

Subclassing Notes

The NSTextStorage class implements change management (via the beginEditing() and endEditing() methods), verification of attributes, delegate handling, and layout management notification. The one aspect it does not implement is managing the actual attributed string storage, which subclasses manage by overriding the two NSAttributedString primitives:



Subclasses must also override two NSMutableAttributedString primitives:



These primitives should perform the change, then call edited(_:range:changeInLength:) to let the parent class know what changes were made.


Accessing Layout Managers

var layoutManagers: [NSLayoutManager]

The layout managers associated with the text storage object.

func addLayoutManager(NSLayoutManager)

Adds a layout manager to the receiver’s set of layout managers.

func removeLayoutManager(NSLayoutManager)

Removes a layout manager from the receiver’s set of layout managers.

Handling Text Editing Messages

func edited(NSTextStorage.EditActions, range: NSRange, changeInLength: Int)

Tracks changes made to the receiver, allowing the text storage to record the full extent of changes made.

func ensureAttributesAreFixed(in: NSRange)

Ensures that attributes are fixed in the given range.

var fixesAttributesLazily: Bool

A Boolean value indicating whether the text storage object fixes attributes lazily.

func invalidateAttributes(in: NSRange)

Invalidates attributes in the specified range.

func processEditing()

Cleans up changes made to the receiver and notifies its delegate and layout managers of changes.

Determining the Nature of Changes

var editedMask: NSTextStorage.EditActions

A mask describing the kinds of edits pending for the text storage object.

var editedRange: NSRange

The range of text containing changes.

var changeInLength: Int

The difference between the current length of the edited range and its length before editing began.

Setting the delegate

var delegate: NSTextStorageDelegate?

The delegate for the text storage object.

Accessing Scriptable Properties

var attributeRuns: [NSTextStorage]

The text storage contents represented as an array of attribute runs.

var paragraphs: [NSTextStorage]

The text storage contents represented as an array of paragraphs.

var words: [NSTextStorage]

The text storage contents represented as an array of words.

var characters: [NSTextStorage]

The text storage contents represented as an array of characters.

var font: NSFont?

The font associated with the text storage.

var foregroundColor: NSColor?

The color to use when drawing the text.


struct NSTextStorage.EditActions

Constants indicating the types of changes that were made.


class let didProcessEditingNotification: NSNotification.Name

Posted after a text storage finishes processing edits in processEditing().

class let willProcessEditingNotification: NSNotification.Name

Posted before a text storage finishes processing edits in processEditing().

See Also


protocol NSTextStorageDelegate

A set of optional methods implemented by delegates of NSTextStorage objects to handle text-edit processing.

class NSTextContainer

A region where text is laid out.

class NSAttributedString

A string that has associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.