可可 – NSAttributedString initWithHTML字符编码不正确?

可可 – NSAttributedString initWithHTML字符编码不正确?,第1张

概述– [NSMutableAttributedString initWith HTML:documentAttributes:]似乎是mangle特殊字符: NSString *html = @"“Hello” World"; // notice the smart quotesNSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncod – [NSMutableAttributedString initWith HTML:documentAttributes:]似乎是mangle特殊字符:

Nsstring *HTML = @"“Hello” World"; // notice the smart quotesNSData *HTMLData = [HTML dataUsingEnCoding:NSUTF8StringEnCoding];NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:HTMLData documentAttributes:nil];NSLog(@"%@",as);

打印“Hello”世界后跟一些RTF命令.在我的应用程序中,我将归因的字符串转换为RTF,并将其显示在NSTextVIEw中,但字符也在那里损坏.

根据文档,默认编码是UTF-8,但我尝试显式,结果是一样的:

NSDictionary *attributes = @{NSCharacterEnCodingdocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEnCoding]};NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:HTMLData documentAttributes:&attributes];
解决方法 在创建NSData时使用[HTML dataUsingEnCoding:NSUnicodeStringEnCoding],并在将HTML解析为属性字符串时设置匹配的编码选项:

NSCharacterEnCodingdocumentAttribute的文档有点混乱:

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字符编码不正确?全部内容,希望文章能够帮你解决可可 – NSAttributedString initWithHTML字符编码不正确?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029122.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存