UIWebView and local css file(UIWebView 和本地 css 文件)
问题描述
我想为桌面设计一个网页样式,以便它可以在 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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIWebView 和本地 css 文件
基础教程推荐
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
