问题描述
我想为桌面设计一个网页样式,以便它可以在 iPhone 上的 UIWebView 上显示.我无权访问页面来源的 Web 服务器.我想通过以编程方式更改 <link>
样式表元素的 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
执行以下操作.
I do the following with my 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
这篇关于UIWebView 和本地 css 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!