简体   繁体   English

如何在RTF文件中保存HTML文本

[英]How to save HTML text in RTF file

Is there any way to save HTML string in RTF file. 有什么方法可以将HTML字符串保存在RTF文件中。 I have tried the below code. 我尝试了下面的代码。 But RTF file is not opening. 但是RTF文件没有打开。

 [strHTML writeToFile:[DOCUMENT_PATH stringByAppendingPathComponent:@"1.rtf"] atomically:YES encoding:NSUTF8StringEncoding error:nil];

Please guide me. 请指导我。

You can try this 你可以试试这个

Create NSMutableAttributedString; 创建NSMutableAttributedString;

 NSMutableAttributedString *textView=[[NSMutableAttributedString alloc]init];
 [textView appendAttributedString:[[NSAttributedString alloc]initWithString:strHTML]];

Save document type to NSRTFTextDocumentType 将文档类型保存到NSRTFTextDocumentType

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                         NSRTFTextDocumentType,
                                         NSDocumentTypeDocumentAttribute,
                                         [NSNumber numberWithInteger:1],
                                         NSCharacterEncodingDocumentAttribute,
                                         nil];
 NSData *data = [textView dataFromRange:NSMakeRange(0, [textView length])
                                documentAttributes:dict
                                             error:nil];

Finally WriteTO File 最终写入文件

 [data writeToFile:[DOCUMENT_PATH stringByAppendingPathComponent:@"1.rtf"] atomically:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM