-[NSMutableAttributedString initWithHTML:documentAttributes:]
似乎破坏了特殊字符:
NSString *html = @"“Hello” World"; // notice the smart quotes
NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:htmlData documentAttributes:nil];
NSLog(@"%@", as);
那打印“Hello†World
接下来是一些 RTF 命令。在我的应用程序中,我将属性字符串转换为 RTF 并将其显示在NSTextView
,但那里的字符也被损坏了。
根据文档,默认编码是UTF-8,但我尝试明确,结果是相同的:
NSDictionary *attributes = @{NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]};
NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:htmlData documentAttributes:&attributes];
Use [html dataUsingEncoding:NSUnicodeStringEncoding]
创建 NSData 并在将 HTML 解析为属性字符串时设置匹配的编码选项:
的文档NSCharacterEncodingDocumentAttribute
有点令人困惑:
NSNumber,包含一个 int 指定NSStringEncoding
为了
文件;用于读取和写入纯文本文件以及写入 HTML;
纯文本的默认值是默认编码; HTML 的默认值是
UTF-8。
所以,你的代码应该是:
NSString *html = @"“Hello” World";
NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
NSMutableAttributedString *as =
[[NSMutableAttributedString alloc] initWithHTML:htmlData
options: options
documentAttributes:nil];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)