Objective C - How to create rtf from NSAttributedString

前端 未结 1 1052
一整个雨季
一整个雨季 2021-02-10 21:31

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

 NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:@{         


        
相关标签:
1条回答
  • 2021-02-10 22:12

    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

    0 讨论(0)
提交回复
热议问题