带有土耳其语文本的HTML未显示在UIWebview中

HTML with Turkish text not displaying in UIWebview

本文关键字:显示 UIWebview HTML 土耳其语 文本      更新时间:2023-09-26

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

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

这是我用来保存和显示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字符串。

如有任何建议,我们将不胜感激。

谢谢。

这一切都是关于字符集的。在HTML <head>meta内容中使用ISO 8859-9而不是UTF-8作为字符集

这个链接提到了土耳其字符集。

土耳其计算机可以使用ISO 8859-9("拉丁语5")字符集与拉丁语1相同,只是很少使用冰岛语字符"eth"、"thorn"answers"带有尖锐重音的y"替换为所需的土耳其语字符。如果您在中阅读土耳其语文本维基百科,当你看到这些冰岛文字时,它们可能是本应为土耳其用户(使用土耳其电脑的用户可能看不到它们)。如果您将土耳其语文本输入维基百科,注意Bomis服务器将网页标识为ISO8859-1,并且没有办法覆盖这一点,所以即使这些字符对您来说似乎是正确的,它们不是正确编码的非土耳其维基读者的观点。

希望它能有所帮助。

根据上面的答案,我能够在代码中找到这个具有正确方法的链接。

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