【发布时间】: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