我的UIWebView存在问题,它忽略了我的样式。它没有任何样式,一切似乎都有它的默认外观。
关于这个的任何想法?您可以在下面找到代码和生成的HTML。
这是我加载和呈现html的代码:
NSStringEncoding encoding;
NSString* content;
NSString* path = [[NSBundle mainBundle] pathForResource:@"style" ofType:@"css"];
if(path) {
content = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:NULL];
}
UIFont *systemFont = [UIFont systemFontOfSize:14.0f];
NSMutableString *html = [NSMutableString stringWithString: @"<!DOCTYPE html>\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
"<head>\n"
"<meta charset=\"utf-8\">\n"
"<title>Application</title>\n"
"<style type=\"text/css\">\n"];
if (content) {
NSLog(@" content of css file is %@",[content stringByReplacingOccurrencesOfString:@"%FONT%" withString:[systemFont familyName]]);
} else {
content = @"Could not load content";
}
[html appendString:[content stringByReplacingOccurrencesOfString:@"%FONT%" withString:[systemFont familyName]]];
[html appendString:@"</style>\n"
"</head>\n"
"<body class=\"main\">\n"];
content = nil;
path = [[NSBundle mainBundle] pathForResource:self.contentResource ofType:@"html"];
if(path) {
content = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:NULL];
} else {
NSLog(@"Couldn't find content resource: %@", self.contentResource);
}
if (content) {
NSLog(@" content of file is %@",content);
} else {
content = @"Could not load content";
}
NSLog(@" webview of this is %@",self.webView);
//continue building the string
[html appendString:content];
[html appendString:@"</body></html>"];
NSLog(@"Full html: %@", html);
//make the background transparent
[self.webView setBackgroundColor:[UIColor clearColor]];
//pass the string to the webview
[self.webView loadHTMLString:content baseURL:nil];
这是由UIWebView呈现之前生成和转储的HTML(请参阅"Full html: %@"
)。如果我将其粘贴到HTML文档中并在浏览器中查看,一切看起来都很正常,应该如此。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta charset="utf-8">
<title>Application</title>
<style type="text/css">
body,
*,
.main {
font-family: .Helvetica Neue Interface, '.Helvetica Neue Interface';
font-size: 14px;
color: #FF0000;
}
.heading {
color: #FF9900;
}
a,
a:hover,
a:focus,
a:active,
a:visited {
color: #428bca;
}</style>
</head>
<body class="main">
<p>Some text that doesn't get styled</p>
</body></html>
编辑:这是加载的2个文件,它等于content of (css) file is
日志语句的日志输出。
的style.css
body,
*,
.main {
font-family: %FONT%, '%FONT%';
font-size: 14px;
color: #FF0000;
}
.heading {
color: #FF9900;
}
a,
a:hover,
a:focus,
a:active,
a:visited {
color: #428bca;
}
contentResource.html
<h4>Info</h4>
<p>Test test test test test test test test test <a href="#ermergency">ermergency</a>.</p>
<p>Still some testing with this</p>