Adding Attachments to Text

You can embed graphics or other attachments in text in either of two ways: programmatically or directly through user actions. In the programmatic approach, you can add graphic objects using the NSText method replaceCharactersInRange:withRTFD: or through a more specific method defined by a subclass.

An alternate means of adding an image or other attachment to the text is for the user to drag an image or other file directly into the text object. The text object automatically creates an NSTextAttachment object to manage the display of the image. This feature requires a rich text object that has been configured to receive dragged images using the NSText method setImportsGraphics:. (NSTextView and NSTextField also have implementations of this method.)

Images that have been imported can be written as part of an RTFD document. RTFD documents use a file package, or directory, to store the components of the document (the “D” stands for “directory”). The file package has the name of the document plus an .rtfd extension. The file package always contains a file called TXT.rtf for the text of the document, and one or more TIFF or EPS files for the images, plus the files for other attachments. A text object can transfer information in an RTFD document to a file and read it from a file using the NSText methods writeRTFDToFile:atomically: and readRTFDFromFile:.