• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Programmatically putting attributed string RTF on the pasteboard
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programmatically putting attributed string RTF on the pasteboard


  • Subject: Re: Programmatically putting attributed string RTF on the pasteboard
  • From: Ken Thomases <email@hidden>
  • Date: Wed, 11 Mar 2015 03:44:55 -0500

On Mar 11, 2015, at 3:36 AM, Charles Jenkins <email@hidden> 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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Programmatically putting attributed string RTF on the pasteboard
      • From: Charles Jenkins <email@hidden>
References: 
 >Programmatically putting attributed string RTF on the pasteboard (From: Charles Jenkins <email@hidden>)

  • Prev by Date: Programmatically putting attributed string RTF on the pasteboard
  • Next by Date: Re: NSRunningApplication executableURL issue in Swift
  • Previous by thread: Programmatically putting attributed string RTF on the pasteboard
  • Next by thread: Re: Programmatically putting attributed string RTF on the pasteboard
  • Index(es):
    • Date
    • Thread