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

# NSTextAttachment

The values for the attachment characteristics of attributed strings and related objects.

```
class NSTextAttachment
```

## Overview

The <doc://com.apple.documentation/documentation/Foundation/NSAttributedString> class uses text attachment objects as the values for attachment attributes (stored in the attributed string under the <doc://com.apple.documentation/documentation/Foundation/NSAttributedString/Key/attachment> key in Swift or the [`NSAttachmentAttributeName`](/documentation/UIKit/NSAttachmentAttributeName) key in Objective-C).

A text attachment object contains either an <doc://com.apple.documentation/documentation/Foundation/NSData> object or an <doc://com.apple.documentation/documentation/Foundation/FileWrapper> object, which in turn holds the contents of the attached file. The properties of this class configure the appearance of the text attachment in your interface. In macOS, the text attachment also uses a cell object that conforms to the <doc://com.apple.documentation/documentation/AppKit/NSTextAttachmentCellProtocol> protocol to draw the image that represents the text and handles mouse events. For more information about text attachments, see the <doc://com.apple.documentation/documentation/Foundation/NSAttributedString> and <doc://com.apple.documentation/documentation/AppKit/NSTextView>.

In macOS 12 and iOS 15 and later, [`NSTextAttachmentViewProvider`](/documentation/UIKit/NSTextAttachmentViewProvider) and [`NSTextAttachmentLayout`](/documentation/UIKit/NSTextAttachmentLayout) provide additional capabilities to represent document locations in terms of an [`NSTextLocation`](/documentation/UIKit/NSTextLocation) or an [`NSTextRange`](/documentation/UIKit/NSTextRange), and provide support for view-based text attachments.

## Topics

### Initializing a text attachment

  <doc://com.apple.documentation/documentation/AppKit/NSTextAttachment/init(fileWrapper:)>

[`init(data:ofType:)`](/documentation/UIKit/NSTextAttachment/init(data:ofType:))

Creates a text attachment object with the specified data.

[`init(image:)`](/documentation/UIKit/NSTextAttachment/init(image:))

Creates a text attachment object to contain the specified image.

### Defining the attachment’s contents

[`bounds`](/documentation/UIKit/NSTextAttachment/bounds)

The layout bounds of the text attachment’s graphical representation in the text coordinate system.

[`contents`](/documentation/UIKit/NSTextAttachment/contents)

The contents for the text attachment.

[`fileType`](/documentation/UIKit/NSTextAttachment/fileType)

The file type of the contents for the text attachment.

[`image`](/documentation/UIKit/NSTextAttachment/image)

An instance of the relevant image class that represents the contents of the text attachment object.

[`fileWrapper`](/documentation/UIKit/NSTextAttachment/fileWrapper)

The text attachment’s file wrapper.

[`allowsTextAttachmentView`](/documentation/UIKit/NSTextAttachment/allowsTextAttachmentView)

A Boolean value that determines whether the text attachment uses text attachment views.

[`usesTextAttachmentView`](/documentation/UIKit/NSTextAttachment/usesTextAttachmentView)

A Boolean value that indicates whether the text attachment uses text attachment views.

[`lineLayoutPadding`](/documentation/UIKit/NSTextAttachment/lineLayoutPadding)

The layout padding before and after the text attachment bounds.

### Setting the attachment cell

  <doc://com.apple.documentation/documentation/AppKit/NSTextAttachment/attachmentCell>

### Constants

[`character`](/documentation/UIKit/NSTextAttachment/character)

Specifies a character that denotes an attachment.

### Convenience methods

[`registerViewProviderClass(_:forFileType:)`](/documentation/UIKit/NSTextAttachment/registerViewProviderClass(_:forFileType:))

Registers a specific file type with the attachment view provider.

[`textAttachmentViewProviderClass(forFileType:)`](/documentation/UIKit/NSTextAttachment/textAttachmentViewProviderClass(forFileType:))

Returns the text attachment view provider class, if any, for the file type you specify.



---

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)