Objective C - How to create rtf from NSAttributedString

你。 提交于 2019-12-21 05:14:26

问题


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

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

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


回答1:


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];

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

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



来源:https://stackoverflow.com/questions/24134217/objective-c-how-to-create-rtf-from-nsattributedstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!