Copy NSAttributedString to Cardboard

Brand new for Cocoa, and I'm trying to figure out how to copy NSAttributedString to cardboard. I have looked through the docs and not sure if I should use NSPasteboardItem or not.

Here I have to copy the usual NSString:

NSPasteboard *pb = [NSPasteboard generalPasteboard]; NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil]; [pb declareTypes:types owner:self]; [pb setString:@"asdfasdf" forType:NSStringPboardType]; 

How to set NSAttributedString?

thanks

+6
cocoa nsattributedstring
source share
3 answers

You want either NSRTFPboardType or NSRTFDPboardType along with NSAttributedString RTFFromRange:documentAttributes: / RTFDFromRange:documentAttributes: and setData on the file cabinet.

+5
source share

Like Snow Leopard, NSAttributedString (when AppKit is turned on) corresponds to NSPasteboardWriting , so you can simply do this:

 [pb clearContents]; [pb writeObjects:arrayOfAttributedStrings]; 

You can send a message to NSArray a arrayWithObject: if you have only one attribute string that you want to put on the cardboard.

[Change from 2013: use the new syntax @[ myAttributedString ] . Works for any number of objects, although in this context they should still match NSPasteboardWriting.]

This also applies to NSString. Find the AppKit headers for "NSPasteboardWriting" to find all the standard Cocoa classes that support it.

+8
source share
 NSPasteboard *paste = [NSPasteboard generalPasteboard]; [paste clearContents]; [paste declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; NSMutableAttributedString *aString;// init some string BOOL success = [paste writeObjects:[NSArray arrayWithObject:aString]]; 
+1
source share

All Articles