我可以使用以下方法将 RTF 字符串转换为属性字符串:
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
现在我怎样才能将attributedString转换回rtf字符串?
我可以使用以下方法将 RTF 字符串转换为属性字符串:
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
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”,但你明白了。
此外,如果你想简单地将其写入磁盘,那么fileWrapperFromRange:documentAttributes:error:甚至可能是一个更好的选择。有关从Attribute String编程指南中读取和写入更多信息。