NSAttributedString initWithHTML字符编码不正确? [英] NSAttributedString initWithHTML incorrect character encoding?

查看:103
本文介绍了NSAttributedString initWithHTML字符编码不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-[NSMutableAttributedString initWithHTML:documentAttributes:] 似乎会破坏特殊字符:

-[NSMutableAttributedString initWithHTML:documentAttributes:] seems to mangle special characters:

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 中,但是那里的字符也损坏了。

That prints “Hello†World followed by some RTF commands. In my application, I convert the attributed string to RTF and display it in an NSTextView, but the characters are corrupted there, too.

根据文档,默认编码为UTF-8,但我尝试了明确的编码,结果是相同的:

According to the documentation, the default encoding is UTF-8, but I tried being explicit and the result is the same:

NSDictionary *attributes = @{NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]};
NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:htmlData documentAttributes:&attributes];


推荐答案

使用 [html dataUsingEncoding:创建NSData并在将HTML解析为属性字符串时将匹配的编码选项设置为NSUnicodeStringEncoding]

<$ c的文档$ c> NSCharacterEncodingDocumentAttribute 有点令人困惑:


NSNumber,包含一个指定的int
文件的NSStringEncoding
;用于读写纯文本文件和编写HTML;纯文本的默认
是默认编码; HTML的默认值为
UTF-8。

NSNumber, containing an int specifying the NSStringEncoding for the file; for reading and writing plain text files and writing HTML; default for plain text is the default encoding; default for HTML is 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];

这篇关于NSAttributedString initWithHTML字符编码不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆