如果我创建它们呈现的内联样式,但是当尝试在xCode中的同一组和目录中引用css文件时,它们不会呈现。
以下是我的工作:
<html>
<head>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Hello, world! Learn</h1>
<p>
<a href="myscheme://advertising" class="buttonStyle">Click me!</a>
</p>
</body>
</html>
然后我有一个名为main.css的css文件,如下所示:
body {background-color:#b0c4de;}
p {background-color:#e0ffff;}
有谁知道为什么这不会渲染css?我是否遗漏了HTML标题中的重要内容?
以下是我首先调用HTML的方法:
- (void)viewDidAppear:(BOOL)animated
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"learn"
ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[theWebView loadHTMLString:htmlString baseURL:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:htmlFile]];
[theWebView loadRequest:request];
}
答案 0 :(得分:8)
问题在于
[theWebView loadHTMLString:htmlString baseURL:nil];
由于baseURL为nil,因此Web视图无法确定css文件的位置。不要乱用手动创建数据并加载它,而是将HTML文件的路径直接传递给UIWebView:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"learn" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *rq = [NSURLRequest requestWithURL:url];
[webView loadRequest:rq];