Cocoa: custom attachment in a text view

I recently posted a query to CocoaDev asking for help with inserting a custom attachment cell into a text view. I had spent quite some time investigating and experimenting, and searches showed several people who had the same question, but no satisfactory answers. Some comments I read suggested subclassing NSTextView, which I tried, but not very satisfactorily.

Fortunately, Douglas Davidson was kind enough to point me in the right direction, combined with helpful off-list discussion with another developer who was working on much the same problem.

I thought I'd share my solution here, in case it helps anyone else. It turned out to be easier than I'd expected. Note: this is written for Leopard with Garbage Collection, so doesn't have releases etc.

Firstly, a file wrapper is used to create a placeholder TIFF with a special "sub-extension" to identify it. I could store any kind of data, but I use a placeholder image so if the user pastes the text into another app, it shows up as something nicer than a generic document icon. The identifier is a unique reference to the data that the attachment represents:

- (NSFileWrapper *)fileWrapperWithIdentifier:(NSString *)identifier;
{
    NSString *wrapName = [[identifier stringByAppendingPathExtension:@"myspecialmarker"] stringByAppendingPathExtension:@"tiff"];
    NSSize size = {100, 18};
    NSData *data = [[NSImage imageNamed:@"AttachmentPlaceholder"] TIFFRepresentation];
    NSFileWrapper *wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
   
    [wrapper setFilename:wrapName];
    [wrapper setPreferredFilename:wrapName];
   
    return wrapper;
}

This method inserts the attachment with the above wrapper and my custom cell:

- (void)insertMarkerWithIdentifier:(NSString *)identifier;
{
   NSFileWrapper *wrapper = [self fileWrapperWithIdentifier:identifier];
   NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper];
   MyAttachmentCell *cell = [MyAttachmentCell new];

   cell.identifier = identifier;
   [attachment setAttachmentCell:cell];

   [[myTextView textStorage] appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
}

I use a couple of text view delegate methods to write the cell's file wrapper to the pasteboard as file contents, allowing it to be copied, dragged, and saved to disk with the text:

- (NSArray *)textView:(NSTextView *)aTextView writablePasteboardTypesForCell:(id <NSTextAttachmentCell>)cell
             atIndex:(NSUInteger)charIndex;
{
   return [NSArray arrayWithObject:NSFileContentsPboardType];
}

- (BOOL)textView:(NSTextView *)aTextView
       writeCell:(id <NSTextAttachmentCell>)cell
         atIndex:(NSUInteger)charIndex
    toPasteboard:(NSPasteboard *)pboard type:(NSString *)type;
{
   if (type == NSFileContentsPboardType)
       [pboard writeFileWrapper:[[cell attachment] fileWrapper]];

   return YES;
}

And to convert the attachment back to my custom cell after pasting/dragging/loading it, the text storage delegate (which is set via [[myTextView textStorage] setDelegate:self];). It looks for TIFF attachments that have my special marker "sub-extension" but aren't using my custom cell, and replaces their cell with my custom one:

- (void)textStorageWillProcessEditing:(NSNotification *)note;
{
   NSAttributedString *text = [myTextView textStorage];

   if ([note object] != text)
       return;

   NSUInteger length = [text length];
   NSRange effectiveRange = NSMakeRange(0, 0);
   id attachment;

   while (NSMaxRange(effectiveRange) < length)
   {
       attachment = [text attribute:NSAttachmentAttributeName atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];

       if (attachment)
       {
           if ([attachment isKindOfClass:[NSTextAttachment class]] &&
               ![[attachment attachmentCell] isKindOfClass:[MyAttachmentCell class]] &&
               [[[[attachment fileWrapper] preferredFilename] pathExtension] isEqualToString:@"tiff"] &&
               [[[[[attachment fileWrapper] preferredFilename] stringByDeletingPathExtension] pathExtension] isEqualToString:@"myspecialmarker"])
           {
               MyAttachmentCell *cell = [MyAttachmentCell new];
               [cell setIdentifier:[[[[attachment fileWrapper] preferredFilename] stringByDeletingPathExtension] stringByDeletingPathExtension]];
               [attachment setAttachmentCell:cell];
           }
       }
   }
}

The custom attachment cell is a subclass of NSTextAttachmentCell, to simply draw in a custom way.

I hope this is helpful to others.