On Nov 12, 2008, at 3:34 AM, Timothy Larkin wrote:

I am building an attributed string for display in a NSTextView. I can include an image in the string by attaching a file wrapper. But what if I have just an NSImage? I know that an image from the clipboard can be inserted in a text view. But how can I do this programatically? Is there a better way than writing the image to a temporary file and then using that file wrapper?

There's some MVC separation going on here. An attachment in the text system consists of the attachment character, with an attachment attribute, whose value is an NSTextAttachment, which has an NSFileWrapper. That's the model part--the part that would get written out to RTFD, for example, or copied and pasted. The actual display, however, is handled by the view layer, using an NSTextAttachmentCell.

Now, there's no requirement that you make use of all of this machinery. If there is an NSFileWrapper, then NSTextAttachment will automatically construct an appropriate NSTextAttachmentCell, using whatever information it can determine about the file--an image for an image file, an icon for a generic file, etc. However, you can override that by setting the text attachment's attachment cell yourself, and if you do that then you don't necessarily need a file wrapper--unless you want the attachment to be saved or copied and pasted with the text. The attachment cell can be any cell that conforms (see the header for details); you can use a stock one on which you set an image or text, or a custom one you create yourself.

However, if you do want to let NSTextAttachment handle the cell creation, you can definitely create an NSFileWrapper from your image without touching the disk. You just need appropriate data for a persistent representation of your image--something like - tiffRepresentation--from which you can create a regular-file NSFileWrapper via -initRegularFileWithContents:. Be sure to call - setPreferredFilename: with an appropriate filename and extension.

Douglas Davidson


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to