Re: Programmatically putting attributed string RTF on the pasteboard
Re: Programmatically putting attributed string RTF on the pasteboard
- Subject: Re: Programmatically putting attributed string RTF on the pasteboard
- From: Charles Jenkins <email@hidden>
- Date: Wed, 11 Mar 2015 07:18:06 -0400
Thank you, Ken.
Because the section on copying to the pasteboard speaks of writing an array of objects to the pasteboard and elsewhere the guide says you provide each possible representation, I thought that meant I “owed” the pasteboard one object for each representation. I couldn’t see how promised types could possibly work, considering that the source data might not exist by the time a consumer accepted it.
So what you say makes it all clear. I put my Clipping object on the pasteboard and forget about it; consumers negotiate with the pasteboard, which only calls pasteboardPropertyListForType: to generate a specific requested representation when needed.
Would it work to use the writing options to specify that all the types are only promised? CLIPPING_UTI must be the first type I specify so my app will receive the most faithful copy of the data; but it would be nice to skip my conversion process entirely in cases where the data gets cut but never pasted or pasted as RTF into another app.
--
Charles
On March 11, 2015 at 04:44:58, Ken Thomases (email@hidden) wrote:
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