Objective C - 如何从NSAttributedString创建rtf [英] Objective C - How to create rtf from NSAttributedString

查看:88
本文介绍了Objective C - 如何从NSAttributedString创建rtf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下命令从rtf字符串转换为属性字符串:

I can convert from rtf string to attributed string using following:

 NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];

现在我怎么能从attributionString转换回rtf字符串?

Now how can i convert back from attributedString to rtf string?

推荐答案

你想使用-dataFromRange:documentAttributes:error:

You want to use -dataFromRange:documentAttributes:error:

NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"YOLO" attributes:nil];
NSData *data = [str dataFromRange:(NSRange){0, [str length]} documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} error:NULL];
[data writeToFile:@"/me.rtf" atomically:YES];

当然你想要一些属性而不是YOLO,但你明白了。

Of course you'd want to have some attributes instead of "YOLO", but you get the idea.

另外,如果你想简单地将它写入磁盘,那么fileWrapperFromRange:documentAttributes:error:
甚至可能是更好的选择。您可以从归因字符串编程指南

Also, if you're looking to simply write this to disk, then fileWrapperFromRange:documentAttributes:error: might even be a better option. You can find more about reading and writing from the Attributed String Programming Guide

这篇关于Objective C - 如何从NSAttributedString创建rtf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆