我花了很多天时间寻找一个解决方案,将带有附件的属性字符串放到NSPasteboard上.
我可以读取带有附件的RTFD文件,修改其文本和属性,然后将其粘贴在NSPasteboard上以便在其他应用程序(例如Mail.app)中使用,并且工作正常.但我想做的还是在文本的某个位置添加图像.我可以将文本作为属性字符串来实现,但如果我尝试插入图像(作为属性字符串的附件),图像永远不会到达(尽管其余部分也是如此).
似乎RTFD有各种各样的风格,我认为我需要的是序列化的.我已经尝试了NSPasteboard声明类型的许多变体,即使使用FileWrappers,但必须缺少重要的东西.无论我做什么,附件似乎永远不会到来.
奇怪的是,如果我读取一个包含图像附件的RTFD文件,修改它并将其粘贴在pasteBoard中,那些原始附件就可以正常工作 - 如果我尝试添加新附件,它们就不会成功.一个例子是读取RTFD文件,处理它,加载粘贴板,并将结果粘贴到邮件中.所有原始文本和图像,以及任何新修改或添加的文本和属性都会显示,但附加的图像根本就丢失了.
这是一些示例代码:
使用一些文本创建一个属性字符串,然后添加一个附加图像,然后添加一些文本,在textView中显示它(一切正常),然后加载粘贴板并粘贴到textEdit或Mail ...附加的图像不存在,其余的是:
// get the image
NSImage *myImage = [[NSImage alloc] initWithData: [window dataWithPDFInsideRect:[theImage frame]]];
// set the image as an attachment
NSTextAttachment *myAttachment = [[NSTextAttachment alloc] init];
NSTextAttachmentCell *myAttachmentCell = [[NSTextAttachmentCell alloc] initImageCell:myImage];
[myAttachment setAttachmentCell:myAttachmentCell];
// put image inside attributed string
NSAttributedString *myImageString = [NSAttributedString attributedStringWithAttachment:myAttachment] ;
// make an attributes dictionary (simply makes text blue) as an example
NSDictionary *myAttributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor blueColor], NSForegroundColorAttributeName,
nil];
// and add some …Run Code Online (Sandbox Code Playgroud)