Cocoa NSAttributedString initWithHTML字符编码不正确?
Cocoa NSAttributedString initWithHTML字符编码不正确?,cocoa,nsattributedstring,Cocoa,Nsattributedstring,-[NSMutableAttributedString initWithHTML:documentAttributes:][/code>似乎会损坏特殊字符: NSString *html = @"“Hello” World"; // notice the smart quotes NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding]; NSMutableAttributedString *as = [[NSMutable
-[NSMutableAttributedString initWithHTML:documentAttributes:][/code>似乎会损坏特殊字符:
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];
创建NSData时使用[html dataUsingEncoding:nsUnicoding]
,将html解析为属性字符串时设置匹配编码选项:
NSCharacterEncodingDocumentAttribute
的文档有点混乱:
NSNumber,包含一个int,指定
文件用于读写纯文本文件和编写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];
前面的答案在这里有效,但大部分是偶然的
使用NSUTF16StringEncoding
生成一个NSData
可能会起作用,因为该常数是NSUTF16StringEncoding
的别名,系统很容易识别UTF-16。比UTF-8容易,UTF-8显然被识别为ASCII的其他超集(在您的例子中,它看起来像NSWindowsCP1252StringEncoding
,可能是因为它是为数不多的基于ASCII的编码,具有0x8和0x9的映射)
在引用NSCharacterEncodingDocumentAttribute
的文档时,这个答案是错误的,因为“属性”是从-initWithHTML
中得到的。这就是为什么它是NSDictionary**
而不仅仅是NSDictionary*
。您可以传入一个指向NSDictionary*
的指针,然后会得到TopMargin/BottomMargin/LeftMargin/RightMargin、PaperSize、DocumentType、UTI等键。您试图通过“属性”字典传入的任何值都将被忽略
您需要使用“选项”来传入值,相关的选项键是NSTextEncodingNameDocumentOption
,它没有记录的默认值。它将字节传递给WebKit进行解析,因此如果您没有指定编码,那么您可能会得到WebKit的编码猜测启发
要确保NSData
和NSAttributedString
之间的编码类型匹配,您应该执行以下操作:
NSString *html = @"“Hello” World";
NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSMutableAttributedString *as =
[[NSMutableAttributedString alloc] initWithHTML:htmlData
options:@{NSTextEncodingNameDocumentOption: @"UTF-8"}
documentAttributes:nil];
公认答案的Swift版本为:
let htmlString: String = "Hello world contains html</br>"
let data: Data = Data(htmlString.utf8)
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
let attributedString = try? NSAttributedString(data: data,
options: options,
documentAttributes: nil)
让htmlString:String=“Hello world包含html”
let data:data=data(htmlString.utf8)
let选项:[NSAttributedString.DocumentReadingOptionKey:Any]=[
.documentType:nsAttributeString.documentType.html,
.characterEncoding:String.Encoding.utf8.rawValue
]
让attributedString=试试?NSAttribute字符串(数据:数据,
选项:选项,
文件属性:无)
太好了。谢谢你。(y) 你应该先试试另一个答案,以防他们对我的答案是正确的,只是碰巧起了作用。我还没有机会亲自测试它-他们部分是对的。这是相同的答案(或多或少),它是有效的。我把这个答案说得更清楚了,因为它被标记为正确。对我来说真的很有用,只需添加NSCharacterEncodingDocumentAttribute,然后它就会显示OK。我不认为这是另一个答案的建议。它只是不完整。事实上,这是正确的答案。另一个答案只有在-initWithHTML
偶然检测到正确的编码时才起作用。使用选项是正确的选择。谢谢