如何在Xcode中添加外部样式表到UIWebView? [英] How can I add an external stylesheet to a UIWebView in Xcode?
本文介绍了如何在Xcode中添加外部样式表到UIWebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经查看了各种类似的问题和答案,但仍然无法使其正常工作,因此我要添加自己的问题:
I have looked at various similar questions and answers and still cannot get this to work, so I'm adding my own question:
我在玩UIWebView 。我可以创建一个工作的html页面与内联CSS。我不知道如何在应用资源组中的文件中加载CSS。
I'm playing with UIWebView. I can create a working html page with inline CSS. I cannot figure out how to get the CSS to load if it is in a file in the app resources group.
我的代码是:
NSString *path = [[NSBundle mainBundle] pathForResource:@"webViewPage2" ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
[self.webView loadHTMLString:htmlString baseURL:nil];
[htmlString release];
我的html调用是这样:
And my html call is this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" type="text/css" media="only screen and (max-device-width: 480px)" href="/greek123.css" />
</head>
<body style="background-color: transparent;">
<h2>Some Title</h2>
<p>We can put instructions here. Such as:
"uh-oh You should not have pushed that button!"
</p>
</body>
</html>
我会感谢任何想法或解决方案。非常感谢!
I would appreciate any ideas or solutions. Many thanks!
推荐答案
将UIWebView的baseURL更改为mainbundle的网址。
change the baseURL of the UIWebView to the url of your mainbundle.
NSURL *mainBundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:htmlString baseURL:mainBundleURL];
这篇关于如何在Xcode中添加外部样式表到UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文