macos - 将 RTF 文件与 Cocoa 合并

标签 macos cocoa merge rtf

如何做到这一点?我知道仅仅合并 NSData 是行不通的。

最佳答案

使用 NSAttributedString 应该可以工作,如下所示的代码

这是将多个 RTF 文件合并在一起的一种非常快速但肮脏的方法

- (void)mergeRTF:(NSURL*)rtf1 :(NSURL*)rtf2 :(NSURL*)merged {
    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setObject:[NSNumber numberWithUnsignedInteger:NSUTF8StringEncoding]
                forKey:NSCharacterEncodingDocumentOption];
    NSDictionary *docAttrs = nil;
    NSError* error = nil;

    NSAttributedString *rtfText1 = [[NSAttributedString alloc] initWithURL:rtf1
                                                               options:options
                                                    documentAttributes:&docAttrs
                                                                 error:&error];

    NSAttributedString *rtfText2 = [[NSAttributedString alloc] initWithURL:rtf2
                                                                options:options
                                                     documentAttributes:&docAttrs
                                                                  error:&error];
    NSMutableAttributedString* whole = [[NSMutableAttributedString alloc] initWithAttributedString:rtfText1];
    [whole appendAttributedString:rtfText2];
    NSData* data = [whole RTFFromRange:NSMakeRange(0, whole.length) documentAttributes:nil];

    [data writeToURL:merged atomically:YES];
}

关于macos - 将 RTF 文件与 Cocoa 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013673/

相关文章:

windows - 与 Windows 相比,为什么 Mac 上可用的独特颜色数量似乎有所不同?

c++ - 无法在 OS/X 中找到 C++ 标准库的位置?

macos - Flutter 桌面嵌入

objective-c - NSStatusItem 始终保留?

ruby-on-rails - 如何将文本合并在一起?就像版本控制系统一样?

python - pandas merge_asof keys must be sorted 排序后出现错误

mysql - 未获取 MySQL max_allowed_pa​​cket 的配置 (Mac OSX)

cocoa - 如何使用 NSFileManager 复制 UNIX 可执行文件?

cocoa - 这个键盘快捷键控制是什么?

python - 将数据框与分钟和日期索引相结合