<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/NSTextStorage",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextStorage"
  },
  "title" : "NSTextStorage"
}
-->

# NSTextStorage

The fundamental storage mechanism of TextKit that contains the text managed by the system.

```
class NSTextStorage
```

## Overview

[`NSTextStorage`](/documentation/UIKit/NSTextStorage) is a semi-concrete subclass of <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString> that adds behavior for managing a set of client [`NSLayoutManager`](/documentation/UIKit/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.

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

In macOS, this class also defines properties for getting and setting scriptable attributes of [`NSTextStorage`](/documentation/UIKit/NSTextStorage) objects. Unless you’re dealing with scriptability, you shouldn’t access these properties directly. In particular, using the <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/characters>, <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/words>, or <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/paragraphs> properties is an inefficient way to manipulate the text storage, since accessing these properties involves the creation of many objects. Instead, use the text access methods defined by <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString>, <doc://com.apple.documentation/documentation/Foundation/NSAttributedString>, <doc://com.apple.documentation/documentation/Foundation/NSMutableString>, and <doc://com.apple.documentation/documentation/Foundation/NSString> to perform character-level manipulation.

### Subclassing Notes

The [`NSTextStorage`](/documentation/UIKit/NSTextStorage) class implements change management through the <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString/beginEditing()> and <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString/endEditing()> methods, as well as verification of attributes, delegate handling, and layout management notification. The one aspect it doesn’t implement is managing the actual attributed string storage, which subclasses manage by overriding the two <doc://com.apple.documentation/documentation/Foundation/NSAttributedString> primitives:

- <doc://com.apple.documentation/documentation/Foundation/NSAttributedString/string>
- <doc://com.apple.documentation/documentation/Foundation/NSAttributedString/attributes(at:effectiveRange:)>

Subclasses must also override two <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString> primitives:

- <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString/replaceCharacters(in:with:)-6oq9r>
- <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString/setAttributes(_:range:)>

These primitives should perform the change, then call [`edited(_:range:changeInLength:)`](/documentation/UIKit/NSTextStorage/edited(_:range:changeInLength:)) to let the parent class know there are changes.

## Topics

### Processing the editing actions

[`var delegate: (any NSTextStorageDelegate)?`](/documentation/UIKit/NSTextStorage/delegate)

The delegate for the text storage object.

[`protocol NSTextStorageDelegate`](/documentation/UIKit/NSTextStorageDelegate)

The optional methods that delegates of text storage objects implement to handle text-edit processing.

### Accessing the layout managers

[`var layoutManagers: [NSLayoutManager]`](/documentation/UIKit/NSTextStorage/layoutManagers)

The layout managers for the text storage object.

[`func addLayoutManager(NSLayoutManager)`](/documentation/UIKit/NSTextStorage/addLayoutManager(_:))

Adds a layout manager to the text storage object’s set of layout managers.

[`func removeLayoutManager(NSLayoutManager)`](/documentation/UIKit/NSTextStorage/removeLayoutManager(_:))

Removes a layout manager from the text storage object’s set of layout managers.

### Managing edits

[`func edited(NSTextStorage.EditActions, range: NSRange, changeInLength: Int)`](/documentation/UIKit/NSTextStorage/edited(_:range:changeInLength:))

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

[`func processEditing()`](/documentation/UIKit/NSTextStorage/processEditing())

Cleans up changes to the text storage object and notifies its delegate and layout managers of changes.

### Fixing the string attributes

[`func invalidateAttributes(in: NSRange)`](/documentation/UIKit/NSTextStorage/invalidateAttributes(in:))

Invalidates attributes in the specified range.

[`func ensureAttributesAreFixed(in: NSRange)`](/documentation/UIKit/NSTextStorage/ensureAttributesAreFixed(in:))

Ensures that attribute fixing occurs in the specified range.

[`var fixesAttributesLazily: Bool`](/documentation/UIKit/NSTextStorage/fixesAttributesLazily)

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

### Determining the nature of changes

[`var editedMask: NSTextStorage.EditActions`](/documentation/UIKit/NSTextStorage/editedMask)

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

[`var editedRange: NSRange`](/documentation/UIKit/NSTextStorage/editedRange)

The range of text that contains changes.

[`var changeInLength: Int`](/documentation/UIKit/NSTextStorage/changeInLength)

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

### Accessing scriptable properties

  <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/attributeRuns>

  <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/paragraphs>

  <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/words>

  <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/characters>

  <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/font>

  <doc://com.apple.documentation/documentation/AppKit/NSTextStorage/foregroundColor>

### Constants

[`struct EditActions`](/documentation/UIKit/NSTextStorage/EditActions)

Constants that indicate the types of changes.

  <doc://com.apple.documentation/documentation/AppKit/NSTextStorageEditedOptions>

### Notifications

[`class let willProcessEditingNotification: NSNotification.Name`](/documentation/UIKit/NSTextStorage/willProcessEditingNotification)

A notification that posts before a text storage begins processing edits.

[`class let didProcessEditingNotification: NSNotification.Name`](/documentation/UIKit/NSTextStorage/didProcessEditingNotification)

A notification that posts after a text storage finishes processing edits.

### Accessing the storage controller

[`var textStorageObserver: (any NSTextStorageObserving)?`](/documentation/UIKit/NSTextStorage/textStorageObserver)

The observer for the text storage object.

[`protocol NSTextStorageObserving`](/documentation/UIKit/NSTextStorageObserving)

Optional methods that delegates implement to handle editing and transaction processing.

## Relationships

### Conforms To

[`NSCopying`](/documentation/Foundation/NSCopying)

[`NSMutableCopying`](/documentation/Foundation/NSMutableCopying)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSItemProviderReading`](/documentation/Foundation/NSItemProviderReading)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Equatable`](/documentation/Swift/Equatable)

[`NSItemProviderWriting`](/documentation/Foundation/NSItemProviderWriting)

[`NSCoding`](/documentation/Foundation/NSCoding)

### Inherits From

[`NSMutableAttributedString`](/documentation/Foundation/NSMutableAttributedString)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)