On Mar 11, 2015, at 3:36 AM, Charles Jenkins <cejw...@gmail.com> wrote:

> I’m having a bit of difficulty learning how to use the pasteboard. I have a 
> text view which holds the text in a rich attributed string. I determined that 
> the built-in methods for cutting and pasting were screwing up my data because 
> not all attributes survive the cut-and-paste process’s translation to and 
> from RTF for the pasteboard.
> 
> For that reason I created my on custom data class as described in the 
> Pasteboard Programming Guide. The class is called “Clipping,” and of course I 
> defined an Exported UTI to go with it.
> 
> The problem is in my implementation of NSPasteboardWriting. Here are the 
> writable types:
> 
> -(NSArray*)writableTypesForPasteboard:(NSPasteboard*)pasteboard
> {
>   NSMutableArray* arr = [[NSMutableArray alloc] initWithObjects:CLIPPING_UTI, 
> nil];
>   [arr addObjectsFromArray:[self.attrStr 
> writableTypesForPasteboard:pasteboard]];
>   return arr;
> }
> 
> The result is three types: CLIPPING_UTI, RTF, and plain string. The first 
> type allows my data to be copied and pasted intact within the text views of 
> my document windows; followed by the normal types of an attributed string for 
> compatibility with other apps.
> 
> What I don’t know is the right way to put all the promised types on the 
> pasteboard in order. Here’s what I tried that does not work:  
> 
> -(BOOL)copyToPasteboard:(NSPasteboard*)pasteboard
> {
>   [pasteboard clearContents];
>   NSMutableArray* objectsToCopy = [[NSMutableArray alloc] init];
>   for ( NSString* type in [self writableTypesForPasteboard:pasteboard] ) {
>     id data = [self pasteboardPropertyListForType:type];
>     [objectsToCopy addObject:data];
>   }
>   return [pasteboard writeObjects:objectsToCopy];
> }

You have one item to add to the pasteboard.  That item supports multiple types, 
but that's not relevant here.  You want to do:

-(BOOL)copyToPasteboard:(NSPasteboard*)pasteboard
{
  [pasteboard clearContents];
  return [pasteboard writeObjects:@[ self ]];
}

That is, just write your object itself, alone, to the pasteboard.  Once you do 
that, with the -writableTypesForPasteboard: method above and the method below, 
you're good.

> -(id)pasteboardPropertyListForType:(NSString*)type
> {
>   if ( [type isEqualToString:CLIPPING_UTI] ) {
>     return self.xml;
>   } else {
>     return [self.attrStr pasteboardPropertyListForType:type];
>   }
> }

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to