UIWebView 和本地 css 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2032159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:26:13  来源:igfitidea点击:

UIWebView and local css file

cssiphonecocoa-touchuiwebview

提问by Edward Bender

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 hrefattribute of the <link>stylesheet element programmatically.

我想为桌面设计一个网页样式,以便它可以在 iPhone 上的 UIWebView 上呈现。我无权访问页面来源的 Web 服务器。我想通过以编程方式更改样式表元素的href属性来做到这一点<link>

I do the following with my IBOutlet UIWebView *webView.

我用我的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]; 

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

这不起作用。使用[webView stringByEvaluatingJavaScriptFromString:]消息并对正文进行更改backgroundColor确实有效 - 作为练习来查看我是否正确使用了调用。

Am I barking up the wrong tree?

我是不是叫错了树?

回答by user275790

You can load CSS from local project directory

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

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

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

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

回答by Andrew

Unfortunately UIWebViewdoesn't provide an API for intercepting and modifyingthe requests that it makes while loading a resource. This means that any approach you try will be hack-ish to a certain extent.

不幸的是,UIWebView它没有提供用于拦截和修改加载资源时发出的请求的 API 。这意味着您尝试的任何方法在某种程度上都将是hack-ish。

The first approach I would try is:

我会尝试的第一种方法是:

  • Provide your own UIWebViewDelegatedelegate on the view.
  • Implement -webView:shouldStartLoadWithRequest:navigationType:and return NOwhen you spot the CSS requests being made.
  • Once the view has finished loading the main resource, use -stringByEvaluatingJavaScriptFromString:to inject JavaScript into the page that dynamically loads/sets the stylesheet that you want to use.
  • UIWebViewDelegate在视图上提供您自己的委托。
  • 当您发现正在发出的 CSS 请求时实施-webView:shouldStartLoadWithRequest:navigationType:并返回NO
  • 视图加载完主要资源后,使用-stringByEvaluatingJavaScriptFromString:将 JavaScript 注入到动态加载/设置您要使用的样式表的页面中。

In the JavaScript I would create an entirely new <link>element/node on the document instead of trying to modify the existing one.

在 JavaScript 中,我会<link>在文档上创建一个全新的元素/节点,而不是尝试修改现有的元素/节点。

回答by user469350

Your code should work, if you choose index 0 of the found elements:

如果您选择找到的元素的索引 0,您的代码应该可以工作:

    NSString *js = @"document.getElementsByTagName('link')[0].setAttribute('href','";
    NSString *js2 = [js stringByAppendingString:cssPath];
    NSString *finalJS = [js2 stringByAppendingString:@"');"];
    [webview stringByEvaluatingJavaScriptFromString:finalJS];

You missed [0]

你错过了 [0]

回答by Jens

SWIFT 3

斯威夫特 3

If you (like me) only use a html snippet (not a complete web page) you need to add your local css file in the header of the html snippet.

如果您(像我一样)只使用 html 片段(不是完整的网页),则需要在 html 片段的标题中添加本地 css 文件。

I therefore define a html header like this:

因此,我定义了一个这样的 html 标题:

let htmlHeader = "<html> \n <head> \n <link href=\"default.css\" rel=\"stylesheet\" type=\"text/css\" /> \n </head> \n <body> \n"

and a footer like this:

和这样的页脚:

let htmlFooter = "</body> \n </html>"

Then find the baseURL for the css like this:

然后像这样找到 css 的 baseURL:

let baseURLForCss = Bundle.main.url(forResource: "default", withExtension: "css")

And load the html snippet with your own css like this:

并使用您自己的 css 加载 html 片段,如下所示:

webView.loadHTMLString(String.init(format: "%@%@&@", htmlHeader, htmlSnippet, htmlFooter), baseURL: baseURLForCss)