Skip navigation.
 
mlCustom attachment in a text view
FROM : David Sinclair
DATE : Tue Nov 06 22:59:38 2007

Goal: drag a table row into a text view as a custom attachment.

Sounds simple, but it seems harder than it should be.  It's easy 
enough to create a custom attachment via a NSTextAttachmentCell 
subclass, and dragging that (or programmatically inserting it) into 
the text view is also easy... but that leaves two problems: dragging/
copying it gives an exception, and persisting it via Core Data makes 
it vanish.

For the first problem, the recommended solution seems to be to 
subclass NSTextView to override -readablePasteboardTypes, -
writablePasteboardTypes, -readSelectionFromPasteboard:type:, etc to 
add the new pasteboard type.  Which works, mostly, except my 
implementation mangles the text if dragging a block of text containing 
my custom attachment, as opposed to the attachment by itself (which 
works fine).  I'm not sure how to handle the former case.

I haven't solved the second problem, of my custom attachments 
vanishing when the text is saved to the data store.  Looking at the 
NSTextStorage contents after inserting it, the attachment is there, 
but it seems to be stripped out when saving (by encoding as data for a 
binary Core Data attribute).  Inserting an image attachment is saved 
correctly, though.  Which makes me think I need to encode the 
attachment somehow, but an -encodeWithCoder: method in the cell isn't 
called.  Do I need to create a temporary file and use a file wrapper 
(the attachment currently passes nil for -initWithFileWrapper:)?  That 
didn't seem to help, but maybe I didn't do it right.

I've searched the archives, and found several people with similar 
questions, but no answers.  If anyone has any advice or can point me 
to sample code for this, I'd much appreciate it.

--

David Sinclair, Dejal Systems, LLC - <email_removed>
Dejal blog - http://www.dejal.com/blog/
Cocoa code - http://www.dejal.com/developer/
Now on Twitter - http://twitter.com/dejal/

Related mailsAuthorDate
mlCustom attachment in a text view David Sinclair Nov 6, 22:59
mlRe: Custom attachment in a text view Douglas Davidson Nov 6, 23:09
mlRe: Custom attachment in a text view David Sinclair Nov 13, 06:37