很老的问题,但也许该解决方案帮助别人......
的RTFD格式不描述文件,但AA文件包中(这实际上是通过在Mac/iOS的UI修改为一个目录对用户来说显示为单个文件)。
不过幸运的是NSFileWrapper能够创造这样RTFD束:
NSAttributedString* attrString = ...some attributed string with images ...;
NSDictionary* documentAttributes = @{
NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType
};
NSError* error = nil;
NSFileWrapper* fileWrapper = [attrString fileWrapperFromRange:NSMakeRange(0, attrString.length)
documentAttributes:documentAttributes
error:&error];
NSString* documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:@"example.rtfd"];
NSLog(@"Writing rtfd doc to: %@", filePath);
[NSFileManager.defaultManager removeItemAtPath:filePath error:&error];
BOOL result = [fileWrapper writeToURL:[NSURL fileURLWithPath:filePath]
options:NSFileWrapperWritingAtomic
originalContentsURL:nil
error:&error];
完成!
由于RTFD几乎仅限于Mac/iOS格式,因此创建包含图像的RTF文件可能更有趣。
不幸的是,Apple的RTF实现(NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType
)不支持图像。
见我回答另一个问题来解决这个:https://stackoverflow.com/a/29181130/2778898
你检查[文本编辑示例代码(https://developer.apple.com/library/mac/samplecode/TextEdit/Introduction/Intro.html )? TextEdit.app可以创建带有附件的富文本文档。 –
你是指TextEdit应用程序?如果是这样,我意识到这可以做我想做的事情,但我想以编程方式做,以便我的应用程序可以创建相当于打开TextEdit,键入文本并插入图像,而无需用户在TextEdit中执行任何操作。 –
是的,我正在谈论TextEdit应用程序。这是完整的源代码可用作示例代码。如果您正在寻找如何使用RTFD文件的示例,那么从TextEdit源代码开始是合理的。 –