<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextView/init(frame:textContainer:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextView(im)initWithFrame:textContainer:"
  },
  "title" : "init(frame:textContainer:)"
}
-->

# init(frame:textContainer:)

Initializes a text view.

```
init(frame frameRect: NSRect, textContainer container: NSTextContainer?)
```

## Parameters

`frameRect`

The frame rectangle of the text view.

`container`

The text container of the text view.

## Return Value

An initialized text view.

## Discussion

This method is the designated initializer for `NSTextView` objects.

Unlike [`init(frame:)`](/documentation/AppKit/NSTextView/init(frame:)), which builds up an entire group of text-handling objects, you use this method after you’ve created the other components of the text-handling system — a text storage object, a layout manager, and a text container. Assembling the components in this fashion means that the text storage, not the text view, is the principal owner of the component objects.

The [`init(frame:)`](/documentation/AppKit/NSTextView/init(frame:)) initializer uses [`NSLayoutManager`](/documentation/AppKit/NSLayoutManager) by default. When you use this initializer in macOS 12 and later, you have the option to use [`NSTextLayoutManager`](/documentation/AppKit/NSTextLayoutManager) which gives you access to newer TextKit functionality and performance improvements. To use the new layout manager  create instances of [`NSTextLayoutManager`](/documentation/AppKit/NSTextLayoutManager), [`NSTextContainer`](/documentation/AppKit/NSTextContainer), and [`NSTextContentStorage`](/documentation/AppKit/NSTextContentStorage); these manage the view’s text layout, text regions, and backingstore, respectively. The example below shows the order of creation and initialization of these objects, and how configure them to initialize an [`NSTextView`](/documentation/AppKit/NSTextView):

```swift
// The viewWidth and viewBounds are set up elsewhere 
// in the App's initialization.

// Create and initialize the supporting layout, container, and storage management.
let textLayoutManager = NSTextLayoutManager()  
let containerSize = NSSize(width: viewWidth, height: CGFloat.greatestFiniteMagnitude)  
let textContainer = NSTextContainer(size: containerSize)  
textLayoutManager.textContainer = textContainer  
let textContentStorage = NSTextContentStorage()  
textContentStorage.addTextLayoutManager(textLayoutManager)  

let textView = NSTextView(frame: viewBounds, textContainer: textLayoutManager.textContainer)
```

In macOS 11 and earlier, you follow a similar pattern but using [`NSLayoutManager`](/documentation/AppKit/NSLayoutManager) and [`NSTextStorage`](/documentation/AppKit/NSTextStorage) instead:

```swift
// The viewBounds and containerSize are set up elsewhere 
// in the App's initialization.
        
// Create and initialize the supporting layout, container, and storage management.
let textContainer = NSTextContainer(size: containerSize)
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
let textStorage = NSTextStorage()
textStorage.addLayoutManager(layoutManager)
        
let textView = NSTextView(frame: viewBounds, textContainer: textContainer)
```

---

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)