Programmatically putting attributed string RTF on the pasteboard
Programmatically putting attributed string RTF on the pasteboard
- Subject: Programmatically putting attributed string RTF on the pasteboard
- From: Charles Jenkins <email@hidden>
- Date: Wed, 11 Mar 2015 04:36:02 -0400
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];
}
-(id)pasteboardPropertyListForType:(NSString*)type
{
if ( [type isEqualToString:CLIPPING_UTI] ) {
return self.xml;
} else {
return [self.attrStr pasteboardPropertyListForType:type];
}
}
When I run the program and copy something, the second type placed on the pasteboard is NSData containing RTF provided by the attributed string (self.attrStr). When I call [pasteboard writeObjects:objectsToCopy], this message appears in the debugger:
“Instances of class NSConcreteMutableData not valid for Pasteboard -writeObjects:. The class NSConcreteMutableData does not implement the NSPasteboardWriting protocol.”
Sure enough, if I switch to TextEdit and paste, only plain string data appears. RTF styling is completely lost.
It seems to me that NSAtttributedString offers to write public.rtf data, but when asked for pasteboardPropertyListForType:@“public.rtf” it’s returning an NSData that cannot actually be used on the pasteboard.
What am I missing?
I know NSAttributedString implements NSPasteboardWriting, so I could just return the attributed string itself, but then I’d be advertising three data types but providing only two...
—
Charles
_______________________________________________
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