【问题标题】:HTML with Turkish text not displaying in UIWebview带有土耳其语文本的 HTML 未在 UIWebview 中显示
【发布时间】:2015-11-09 10:38:50
【问题描述】:

我的 iOS 项目中有一个 UIWebview,我从 Web 服务加载英语和土耳其语内容。

我用适当的 HTML 标头包装 html 字符串,然后将其保存到本地文件,然后在我的 UIWebview 中显示已保存文件的内容。

这是我用来保存和显示 HTML 的代码。

-(void) prepareHTML:(NSString*) html {
  tableOfcontentsHashMap = [NSMutableDictionary new];

//   html = [NSString stringWithFormat:@"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"></head><body>%@</body></html>",html];

html = [NSString stringWithFormat:@"<html lang=\"fr\"><head><meta content=\"text/html; charset=UTF-8\" http-equiv=\"content-type\"/><meta name=\"viewport\" content=\"width=320; initial-scale=0.5; maximum-scale=1.0; user-scalable=1;\"/></head><body style=\"width:500;font-size:20px;\">%@</body></html>",html];
HTMLDocument *document = [HTMLDocument documentWithString:html];
NSArray *headers = [document nodesMatchingSelector: @"h1,h2"];

NSInteger x = 0;
for (HTMLElement *header in headers) {
    NSMutableOrderedSet *children = [header.parentNode mutableChildren];
    NSString * hash =[NSString stringWithFormat: @"heading_%ld", (long)x];
    HTMLElement *wrapper = [[HTMLElement alloc] initWithTagName:@"div"
                                                     attributes:@{@"id":
                                                                      hash}];
    [children insertObject:wrapper atIndex:[children indexOfObject:header]];
    [tableOfcontentsHashMap setObject:hash forKey:header.textContent];
    x++;
}
NSLog(@"Final HTML: %@",[document serializedFragment]);

[self writeHtmlToFile:[document serializedFragment] onWriteCallback:^(id result) {
    if(result) {
        NSLog(@"FilePath: %@",result);
        htmlFile = result;
        NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];

        [self.webView loadData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];

    } else {
        [Utils showMessageHUDInView:self.view withMessage:@"Error Displaying information" afterError:YES];
    }
}];

}

使用上面的代码,英文的HTML显示得很好,但是土耳其文的HTML即使加载得很好,也完全不显示。

我尝试了几种编码技巧/建议,如代码所示。但我无法让它显示土耳其语 HTML 字符串。

我们将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript html ios objective-c uiwebview


    【解决方案1】:

    这都是关于 charset 的。在HTML &lt;head&gt;meta 内容中使用ISO 8859-9 而不是UTF-8 作为字符集

    这个link 提到土耳其字符集。

    土耳其计算机可以使用字符集 ISO 8859-9(“Latin 5”),它 与拉丁语 1 相同,除了很少使用的冰岛语 字符“eth”、“thorn”和“带有尖音的 y”被替换为 所需的土耳其语字符。如果您正在阅读土耳其语文本 维基百科,当你看到这些冰岛字符时,它们可能是 意味着是土耳其人(使用土耳其计算机的用户可能会或可能会 看不到他们)。如果您将土耳其语文本输入 维基百科,请注意 Bomis 服务器将网页标识为 ISO 8859-1,并且没有办法覆盖它,所以即使这些 字符对您来说是正确的,它们没有正确编码 非土耳其语 Wiki 读者的观点。

    希望它会有所帮助。

    【讨论】:

      【解决方案2】:

      从上面的答案,我能够找到这个link,它有正确的方法,在代码中。

      我还必须使用NSWindowsCP1254StringEncoding 保存我的文件,并在meta 标头中使用windows-1254 字符集。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-19
        • 2015-07-30
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        相关资源
        最近更新 更多