如果我创建了它们呈现的内联样式,但当尝试引用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];
}
发布于 2012-08-14 22:43:56
问题出在
[theWebView loadHTMLString:htmlString baseURL:nil];
由于baseURL为空,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];
https://stackoverflow.com/questions/11954661
复制相似问题