UIWebView和本地css文件 [英] UIWebView and local css file

查看:80
本文介绍了UIWebView和本地css文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为桌面设计一个网页样式,以便在iPhone上的UIWebView上显示。我无法访问该页面源自的Web服务器。我想通过以编程方式更改< link> stylesheet元素的 href 属性来实现。

I want to style a web page meant for the desktop so that it is presentable on a UIWebView on iPhone. I do not have access to the web server from which the pages originate. I would like to do this by changing the href attribute of the <link> stylesheet element programmatically.

我使用 IBOutlet UIWebView * webView 执行以下操作。

NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"MyStyleSheet" 
                                                    ofType:@"css"];
NSString *js = @"document.getElementsByTagName('link').setAttribute('href','";
NSString *js2 = [js stringByAppendingString:cssPath];
NSString *finalJS = [js2 stringByAppendingString:@"');"];

//check element structure
NSString *res = [webView stringByEvaluatingJavaScriptFromString:finalJS]; 

这不起作用。使用 [webView stringByEvaluatingJavaScriptFromString:] 消息并更改主体的 backgroundColor

This does not work. Using the [webView stringByEvaluatingJavaScriptFromString:] message and making a change to the backgroundColor of the body does indeed work - done as an exercise to see if I was using the call correctly.

我是咆哮错误的树吗?

推荐答案

您可以从本地项目目录加载CSS

You can load CSS from local project directory

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

详细信息检查此网站:
http://iphoneincubator.com/blog/windows-views/uiwebview-revisited

detail info check this site : http://iphoneincubator.com/blog/windows-views/uiwebview-revisited

这篇关于UIWebView和本地css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆