Javascript 不在UIWebview中显示土耳其文本的HTML
我在iOS项目中有一个UIWebview,我正在从web服务加载英语和土耳其语内容 我用适当的html头包装html字符串,然后将其保存到本地文件,然后在UIWebview中显示保存文件的内容 下面是我用来保存和显示HTML的代码Javascript 不在UIWebview中显示土耳其文本的HTML,javascript,html,ios,objective-c,uiwebview,Javascript,Html,Ios,Objective C,Uiwebview,我在iOS项目中有一个UIWebview,我正在从web服务加载英语和土耳其语内容 我用适当的html头包装html字符串,然后将其保存到本地文件,然后在UIWebview中显示保存文件的内容 下面是我用来保存和显示HTML的代码 -(void) prepareHTML:(NSString*) html { tableOfcontentsHashMap = [NSMutableDictionary new]; // html = [NSString stringWithFormat:@
-(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];
}
}];
-(void)prepareHTML:(NSString*)html{
tableOfcontentsHashMap=[NSMutableDictionary new];
//html=[NSString stringWithFormat:@“%@”,html];
html=[NSString stringWithFormat:@“%@”,html];
HTMLDocument*document=[HTMLDocument documentWithString:html];
NSArray*标题=[文档节点匹配选择器:@“h1,h2”];
NSInteger x=0;
用于(标题中的HTMLElement*标题){
NSMutableOrderedSet*children=[header.parentNode mutableChildren];
NSString*哈希=[NSString stringWithFormat:@“heading_%ld”,(长)x];
HtmleElement*包装器=[[HtmleElement alloc]initWithTagName:@“div”
属性:@{@“id”:
hash}];
[children-insertObject:wrapper-atIndex:[children-IndexOfoObject:header];
[TableOfContentsHashMapSetObject:hash-forKey:header.textContent];
x++;
}
NSLog(@“最终HTML:%@,[document serializedFragment]);
[self-writeHtmlToFile:[document serializedFragment]onWriteCallback:^(id结果){
如果(结果){
NSLog(@“文件路径:%@”,结果);
htmlFile=结果;
NSString*htmlString=[NSString STRINGWITHCONTENTSOFILE:htmlFile编码:NSUTF8STRING编码错误:nil];
[self.webView loadData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]MIMEType:@“text/html”textcodedingname:@“UTF-8”baseURL:nil];
}否则{
[Utils showMessageHUDInView:self.view with message:@“错误显示信息”后错误:是];
}
}];
}
在上面的代码中,带有英语的HTML显示得很好,但是带有土耳其文本的HTML根本不显示,即使它加载得很好
我尝试了几种编码技巧/建议,如代码中所示。但我无法让它显示土耳其HTML字符串
如有任何建议,我们将不胜感激
谢谢。都是关于charset的。使用
ISO 8859-9
而不是UTF-8
作为HTML的meta
内容中的字符集
这是为土耳其字符集提到的
土耳其计算机可能使用字符集ISO 8859-9(“拉丁语5”),其中
与拉丁语1相同,只是很少使用冰岛语
字符“eth”、“thorn”和“带有尖锐口音的y”替换为
所需的土耳其字符。如果你正在用英语阅读土耳其语文本
维基百科,当你看到这些冰岛文字时,它们可能是
指土耳其用户(使用土耳其计算机的用户可能会或可能会
看不清楚)。如果您将土耳其语文本输入到
Wikipedia,请注意,Bomis服务器将网页标识为ISO
8859-1,没有办法覆盖这个,所以即使这些
字符在您看来是正确的,它们没有正确编码
非土耳其维基读者的观点
希望它能有所帮助。从上面的答案中,我能够在代码中找到正确的方法
我还必须使用NSWindowsCP1254StringEncoding
保存文件,并在meta
标题中使用windows-1254
字符集