Question

I have an NSAttributedString that I want to write back into the pasteboard.

The general NSPasteboard has different types, the NSAttributedString with attachments does fit to NSPasteboardTypeRTFD

I know how to write into the pasteboard:

    NSData * __strong newContent = ... // how?
    NSPasteboard * __strong pboard = [NSPasteboard generalPasteboard];
    NSString * __strong type = NSPasteboardTypeRTFD;
    [pboard setData:newContent forType:type];

But how to convert from a given NSAttributedString two a RTF NSData (which should then fit the type readable by the pasteboard)?

Was it helpful?

Solution

You can do this with RTFDFromRange:documentAttributes:

NSAttributedString *text;
...
NSData *rtfd = [text RTFDFromRange:NSMakeRange(0, text.length)
                documentAttributes:nil];

OTHER TIPS

what do you want the data to be, do you just want the text?
Then you can convert to an NSString with -string

Edit: I couldn't find the methods because they are in a category: NSAttributedString Application Kit Additions

Thank to @CRD for pointing me there.

I think it is interesting that it states for -RTFDFromRange:documentAttributes: it states that the documentAttributes is a required dictionary, then it says:

If there are no document-level attributes, dict can be nil.

The companion guide can be found here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top