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

标签 javascript html ios objective-c uiwebview

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

我用适当的 HTML header 包装 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 字符串。

我们将不胜感激来自此处的任何建议。

谢谢。

最佳答案

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

这个link提及土耳其语字符集。

Turkish computers may use character set ISO 8859-9 ("Latin 5"), which is identical to Latin 1 except that the rarely-used Icelandic characters "eth", "thorn", and "y with acute accent" are replaced with the needed Turkish characters. If you are reading Turkish text in Wikipedia, when you see these Icelandic characters they are probably meant to be the Turkish ones (users with Turkish computers may or may not see them properly). If you are entering Turkish text into Wikipedia, be aware that the Bomis server identifies web pages as ISO 8859-1, and there is no way to override this, so even if these characters appear correct to you, they are not properly encoded from the point of view of a non-Turkish Wiki reader.

希望对您有所帮助。

关于javascript - 带有土耳其语文本的 HTML 未显示在 UIWebview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33607310/

相关文章:

html - CSS 属性仅在 IE 中被覆盖

ios - '-[UITextField 日期] : unrecognized selector sent to instance 0x7f89277121c0'

ios - 在 UIScrollView 中调整 View 大小

Javascript拼接方法困惑

JavaScript 延迟

javascript - 单击框内的任意位置,关闭

javascript - 使用lazysizes检测DOM节点何时进入视口(viewport)

html - CSS 底部属性不适用于百分比(仅像素)

html - 如何更改文本中带 * 的标签的颜色

ios - FBRequestConnection startForMyFriendsWithCompletionHandler 不带所有安装了该应用程序的 friend - iOS