Class

NSTextStorage

The NSTextStorage class defines the fundamental storage mechanism of TextKit. This class 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.

Overview

An NSTextStorage 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:

string

attributes(at:effectiveRange:)

Subclasses must also override two NSMutableAttributedString primitives:

replaceCharacters(in:with:)

setAttributes(_:range:)

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

Symbols

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(NSTextStorageEditActions, 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: NSTextStorageEditActions

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.

Constants

NSTextStorageEditActions

Constants indicating the types of changes that were made.

Notifications

static let NSTextStorageDidProcessEditing: NSNotification.Name

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

static let NSTextStorageWillProcessEditing: NSNotification.Name

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