Custom attachment in a text view

  • 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 - <dev...>
    Dejal blog - http://www.dejal.com/blog/
    Cocoa code - http://www.dejal.com/developer/
    Now on Twitter - http://twitter.com/dejal/
  • On Nov 6, 2007, at 1:59 PM, David Sinclair wrote:

    > 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.

    Here's what I sent to this list in response to another question
    earlier today:

    "The issue here has to do with the formats used to store text on the
    pasteboard.  Copy/paste can potentially take place between any two
    applications, so the pasteboard generally uses application-independent
    formats for the interchange of data.  In this case, attachments are
    stored for pasteboard purposes as file contents.  One option for you
    is to decide on a file format to represent the contents of your custom
    attachments, and use that as the contents of the file wrapper for copy/
    paste purposes.  In this, you should consider what you want to happen
    when text is copied and pasted from your application to another
    application--is there some suitable representation for your custom
    attachments that other applications would understand?  Your
    application could then recognize these files by some means--by
    contents, or perhaps by extension--when they are pasted in, and
    substitute your custom attachment."

    This would be applicable to your question about dragging or copy/
    pasting a block of text containing your custom attachment.  Your
    second question is probably the same problem, since it has to do with
    the storage of text in persistent formats.

    Douglas Davidson
  • On Nov 6, 2007, at 13:59:38, David Sinclair wrote:

    > 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 archives: here's a description of what I ended up with, thanks
    to Douglas Davidson's comments and off-list discussion with another
    developer that was working on much the same problem:

    <http://www.dejal.com/blog/2007/11/cocoa-custom-attachment-text-view>

    --

    David Sinclair, Dejal Systems, LLC - <dev...>
    Dejal blog - http://www.dejal.com/blog/
    Cocoa code - http://www.dejal.com/developer/
    Now on Twitter - http://twitter.com/dejal/
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today