Has*_*ssy 4 rtf objective-c nsattributedstring ios
我可以使用以下命令从rtf字符串转换为属性字符串:
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)
现在我怎么能从attributionString转换回rtf字符串?
Luc*_*ugh 13
您想使用-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];
Run Code Online (Sandbox Code Playgroud)
当然你想要一些属性而不是"YOLO",但你明白了.
此外,如果您只想将其写入磁盘,那么fileWrapperFromRange:documentAttributes:error:甚至可能是更好的选择.您可以从" 归因字符串编程指南"中找到有关读取和写入的更多信息