Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何将NSAttributedString转换为NSData?_Objective C_Nsattributedstring_Nspasteboard_Osx Mavericks - Fatal编程技术网

Objective c 如何将NSAttributedString转换为NSData?

Objective c 如何将NSAttributedString转换为NSData?,objective-c,nsattributedstring,nspasteboard,osx-mavericks,Objective C,Nsattributedstring,Nspasteboard,Osx Mavericks,我有一个NSAttributedString,我想把它写回粘贴板 常规的NSPasteboard有不同的类型,带有附件的NSAttributedString适合NSPasteboardTypeRTFD 我知道如何在粘贴板上书写: NSData * __strong newContent = ... // how? NSPasteboard * __strong pboard = [NSPasteboard generalPasteboard]; NSString * __

我有一个
NSAttributedString
,我想把它写回粘贴板

常规的
NSPasteboard
有不同的类型,带有附件的
NSAttributedString
适合
NSPasteboardTypeRTFD

我知道如何在粘贴板上书写:

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

但是如何从一个给定的
NSAttributedString
两个RTF NSData(然后应该适合粘贴板可读的类型)?

您希望数据是什么,您只需要文本吗?
然后您可以使用
-string
转换为NSString

编辑:我找不到这些方法,因为它们位于一个类别中:

感谢@CRD为我指明了方向

我认为有趣的是,它声明了
-RTFDFromRange:documentAttributes:
它声明
documentAttributes
是一个必需的字典,然后它说:

如果没有文档级属性,dict可以为nil


可以找到配套指南

您可以使用
RTFDFromRange:documentAttribute:

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

RTFDFromRange:documentAttributes:
也许?你能给出一个完整的
nsattributestring
的示例代码和范围示例吗?nsattributestring可以是什么,例如带有图像附件的样式文本