Mac Developer Library

Developer

AppKit Framework Reference NSTextAttachment Class Reference

Options
Deployment Target:

On This Page
Language:

NSTextAttachment

NSTextAttachment objects are used by the NSAttributedString class cluster as the values for attachment attributes (stored in the attributed string under the key named NSAttachmentAttributeName). The objects you create with this class are referred to as text attachment objects, or when no confusion will result, as text attachments or merely attachments.

A text attachment object contains either an NSData object or an NSFileWrapper 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. On OS X, the text attachment also uses a cell object conforming to the NSTextAttachmentCell protocol to draw and handle mouse events.

For more information about text attachments, see the NSAttributedString Class Reference and NSTextView Class Reference.

  • Initializes a newly allocated NSTextAttachment object to contain the given file wrapper.

    Declaration

    Swift

    convenience init(fileWrapper fileWrapper: NSFileWrapper?)

    Objective-C

    - (instancetype)initWithFileWrapper:(NSFileWrapper *)aWrapper

    Parameters

    aWrapper

    The file wrapper for the receiver.

    Return Value

    The receiver initialized to contain aWrapper and use an NSTextAttachmentCell as its attachment cell.

    Discussion

    This method is the designated initializer for the NSTextAttachment class on OS X.

    If aWrapper contains an image file that the receiver can interpret as an NSImage object, sets the attachment cell’s image to the NSImage rather than to the icon of aWrapper.

    Availability

    Available in OS X v10.0 and later.

  • Initializes a new NSTextAttachment object with the given data.

    Declaration

    Swift

    init(data contentData: NSData?, ofType uti: String?)

    Objective-C

    - (instancetype)initWithData:(NSData *)contentData ofType:(NSString *)uti

    Parameters

    contentData

    Data to use for the text attachment contents. Can be nil.

    uti

    A uniform type identifier specifying the data type of the attachment contents. Can be nil.

    Return Value

    A new NSTextAttachment object.

    Discussion

    This method is the designated initializer for the NSTextAttachment class on iOS.

    When either contentData or uti is nil, the receiver is consider to be an attachment without document contents. In this case, the NSAttributedString external file writing methods try to save the value of the image property instead.

    Availability

    Available in OS X v10.11 and later.

  • Defines the layout bounds of the receiver's graphical representation in the text coordinate system.

    Declaration

    Swift

    var bounds: NSRect

    Objective-C

    @property NSRect bounds

    Discussion

    The bounds rectangle origin is at the current glyph location on the text baseline. The default value is CGRectZero.

    Availability

    Available in OS X v10.11 and later.

  • Defines the contents for the text attachment.

    Declaration

    Swift

    @NSCopying var contents: NSData?

    Objective-C

    @property(copy) NSData *contents

    Discussion

    Modifying this property has the side effect of invalidating the image property.

    Availability

    Available in OS X v10.11 and later.

  • Defines the file type of the contents for the text attachment.

    Declaration

    Swift

    var fileType: String?

    Objective-C

    @property(copy) NSString *fileType

    Discussion

    Modifying this property has the side effect of invalidating the image property.

    Availability

    Available in OS X v10.11 and later.

  • Image representing the text attachment contents.

    Declaration

    Swift

    var image: NSImage?

    Objective-C

    @property(strong) NSImage *image

    Availability

    Available in OS X v10.11 and later.

  • The receiver’s file wrapper.

    Declaration

    Swift

    var fileWrapper: NSFileWrapper?

    Objective-C

    @property(strong) NSFileWrapper *fileWrapper

    Discussion

    The file wrapper holds the contents of the attached file. In iOS, modifying this property has a side effect of invalidating the image, contents, and fileType properties.

    Availability

    Available in OS X v10.0 and later.

  • This character is used to denote an attachment.

    Declaration

    Swift

    var NSAttachmentCharacter: Int { get }

    Objective-C

    enum { NSAttachmentCharacter = 0xfffc };

    Constants

    • NSAttachmentCharacter

      NSAttachmentCharacter

      Specifies a character that denotes an attachment.

      Available in OS X v10.0 and later.