<!--
{
  "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/NSTextContainer",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextContainer"
  },
  "title" : "NSTextContainer"
}
-->

# NSTextContainer

A region where text layout occurs.

```
class NSTextContainer
```

## Overview

An [`NSLayoutManager`](/documentation/UIKit/NSLayoutManager) uses [`NSTextContainer`](/documentation/UIKit/NSTextContainer) to determine where to break lines, lay out portions of text, and so on. An [`NSTextContainer`](/documentation/UIKit/NSTextContainer) object typically defines rectangular regions, but you can define exclusion paths inside the text container to create regions where text doesn’t flow. You can also subclass to create text containers with nonrectangular regions, such as circular regions, regions with holes in them, or regions that flow alongside graphics.

You can access instances of the [`NSTextContainer`](/documentation/UIKit/NSTextContainer), [`NSLayoutManager`](/documentation/UIKit/NSLayoutManager), and [`NSTextStorage`](/documentation/UIKit/NSTextStorage) classes from threads other than the main thread as long as the app guarantees access from only one thread at a time.

## Topics

### Creating a text container

[`init(size:)`](/documentation/UIKit/NSTextContainer/init(size:))

Initializes a text container with a specified bounding rectangle.

[`init(coder:)`](/documentation/UIKit/NSTextContainer/init(coder:))

Creates a text container from data in an unarchiver.

### Managing text components

[`layoutManager`](/documentation/UIKit/NSTextContainer/layoutManager)

The text container’s layout manager.

[`textLayoutManager`](/documentation/UIKit/NSTextContainer/textLayoutManager)

The [`NSTextLayoutManager`](/documentation/UIKit/NSTextLayoutManager) owning the text container.

[`replaceLayoutManager(_:)`](/documentation/UIKit/NSTextContainer/replaceLayoutManager(_:))

Replaces the layout manager for the group of text system objects that contains the text container.

  <doc://com.apple.documentation/documentation/AppKit/NSTextContainer/textView>

### Defining the container shape

[`size`](/documentation/UIKit/NSTextContainer/size)

The size of the text container’s bounding rectangle.

[`exclusionPaths`](/documentation/UIKit/NSTextContainer/exclusionPaths)

An array of path objects that represents the regions where text doesn’t display in the text container.

[`lineBreakMode`](/documentation/UIKit/NSTextContainer/lineBreakMode)

The behavior of the last line inside the text container.

[`widthTracksTextView`](/documentation/UIKit/NSTextContainer/widthTracksTextView)

A Boolean that controls whether the text container adjusts the width of its bounding rectangle when its text view resizes.

[`heightTracksTextView`](/documentation/UIKit/NSTextContainer/heightTracksTextView)

A Boolean that controls whether the text container adjusts the height of its bounding rectangle when its text view resizes.

### Constraining text layout

[`maximumNumberOfLines`](/documentation/UIKit/NSTextContainer/maximumNumberOfLines)

The maximum number of lines that the text container can store.

[`lineFragmentPadding`](/documentation/UIKit/NSTextContainer/lineFragmentPadding)

The value for the text inset within line fragment rectangles.

[`lineFragmentRect(forProposedRect:at:writingDirection:remaining:)`](/documentation/UIKit/NSTextContainer/lineFragmentRect(forProposedRect:at:writingDirection:remaining:))

Returns the bounds of a line fragment rectangle inside the text container for the proposed rectangle.

[`isSimpleRectangularTextContainer`](/documentation/UIKit/NSTextContainer/isSimpleRectangularTextContainer)

A Boolean that indicates whether the text container’s region is a rectangle with no holes or gaps, and whose edges are parallel to the text view’s coordinate system axes.

### Deprecated

  <doc://com.apple.documentation/documentation/AppKit/NSTextContainer/init(containerSize:)>

  <doc://com.apple.documentation/documentation/AppKit/NSTextContainer/lineFragmentRect(forProposedRect:sweepDirection:movementDirection:remaining:)>

  <doc://com.apple.documentation/documentation/AppKit/NSTextContainer/contains(_:)>

  <doc://com.apple.documentation/documentation/AppKit/NSTextContainer/containerSize>



---

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)