如何在程序生成的iPhone UIWebView HTML中包含CSS资源?

时间:2021-09-26 09:48:01

I am programmatically generating HTML and want to link to a CSS file that I've included in my Resources folder. I am using a baseURL of nil, and my CSS file is at the top level of the project file, but this "link" is definitely not working (ie, the CSS file is clearly not being loaded/found when the UIWebView displays the HTML).

我是以编程方式生成HTML,并希望链接到我已包含在我的Resources文件夹中的CSS文件。我使用的是nUR的baseURL,我的CSS文件位于项目文件的顶层,但是这个“链接”肯定不起作用(即,当UIWebView显示HTML时,CSS文件显然没有被加载/找到)。

Is it possible to do this? Or will I need to put the CSS file on the web somewhere and link to it via a URL? (I can see the advantage of doing this -allowing the app to change its style without redistributing the app).

是否有可能做到这一点?或者我需要将CSS文件放在网络上并通过URL链接到它? (我可以看到这样做的好处 - 让应用程序在不重新分发应用程序的情况下改变其风格)。

Thanks

2 个解决方案

#1


5  

Thanks everyone! Here's what I did, works great.

感谢大家!这就是我做的,效果很好。

NSBundle *bundle = [NSBundle mainBundle];
NSURL *resourceBaseURL = [NSURL fileURLWithPath:[bundle bundlePath]];
[self.webView loadHTMLString:htmlString baseURL:resourceBaseURL];

#2


1  

Set the base URL to the bundle path:

将基本URL设置为包路径:

NSBundle *bundle = [NSBundle mainBundle];
NSURL *baseURL = [NSURL fileURLWithPath:[bundle bundlePath]];

#1


5  

Thanks everyone! Here's what I did, works great.

感谢大家!这就是我做的,效果很好。

NSBundle *bundle = [NSBundle mainBundle];
NSURL *resourceBaseURL = [NSURL fileURLWithPath:[bundle bundlePath]];
[self.webView loadHTMLString:htmlString baseURL:resourceBaseURL];

#2


1  

Set the base URL to the bundle path:

将基本URL设置为包路径:

NSBundle *bundle = [NSBundle mainBundle];
NSURL *baseURL = [NSURL fileURLWithPath:[bundle bundlePath]];