<!--
{
  "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

[`delegate`](/documentation/UIKit/NSTextStorage/delegate)

The delegate for the text storage object.

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

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

### Accessing the layout managers

[`layoutManagers`](/documentation/UIKit/NSTextStorage/layoutManagers)

The layout managers for the text storage object.

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

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

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

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

### Managing edits

[`-  edited:range:changeInLength:`](/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.

[`-  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

[`-  invalidateAttributesInRange:`](/documentation/UIKit/NSTextStorage/invalidateAttributes(in:))

Invalidates attributes in the specified range.

[`-  ensureAttributesAreFixedInRange:`](/documentation/UIKit/NSTextStorage/ensureAttributesAreFixed(in:))

Ensures that attribute fixing occurs in the specified range.

[`fixesAttributesLazily`](/documentation/UIKit/NSTextStorage/fixesAttributesLazily)

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

### Determining the nature of changes

[`editedMask`](/documentation/UIKit/NSTextStorage/editedMask)

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

[`editedRange`](/documentation/UIKit/NSTextStorage/editedRange)

The range of text that contains changes.

[`changeInLength`](/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

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

Constants that indicate the types of changes.

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

### Notifications

[`NSTextStorageWillProcessEditingNotification`](/documentation/UIKit/NSTextStorage/willProcessEditingNotification)

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

[`NSTextStorageDidProcessEditingNotification`](/documentation/UIKit/NSTextStorage/didProcessEditingNotification)

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

### Accessing the storage controller

[`textStorageObserver`](/documentation/UIKit/NSTextStorage/textStorageObserver)

The observer for the text storage object.

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

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

## Relationships

### Conforms To

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

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

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

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

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

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

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

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

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

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

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

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

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

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

### 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)