How can I add an external stylesheet to a UIWebView in Xcode?(如何在 Xcode 中向 UIWebView 添加外部样式表?)
问题描述
我查看了各种类似的问题和答案,但仍然无法解决这个问题,所以我添加了我自己的问题:
I have looked at various similar questions and answers and still cannot get this to work, so I'm adding my own question:
我正在玩 UIWebView.我可以使用内联 CSS 创建一个工作 html 页面.如果 CSS 在应用资源组的文件中,我无法弄清楚如何加载它.
I'm playing with UIWebView. I can create a working html page with inline CSS. I cannot figure out how to get the CSS to load if it is in a file in the app resources group.
我的代码是:
NSString *path = [[NSBundle mainBundle] pathForResource:@"webViewPage2" ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
[self.webView loadHTMLString:htmlString baseURL:nil];
[htmlString release];
我的 html 调用是这样的:
And my html call is this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" type="text/css" media="only screen and (max-device-width: 480px)" href="/greek123.css" />
</head>
<body style="background-color: transparent;">
<h2>Some Title</h2>
<p>We can put instructions here. Such as:
"uh-oh You should not have pushed that button!"
</p>
</body>
</html>
如果有任何想法或解决方案,我将不胜感激.非常感谢!
I would appreciate any ideas or solutions. Many thanks!
推荐答案
将 UIWebView 的 baseURL 更改为 mainbundle 的 url.
change the baseURL of the UIWebView to the url of your mainbundle.
NSURL *mainBundleURL = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:mainBundleURL];
斯威夫特 3:
webView.loadHTMLString(contentString, baseURL: Bundle.main.bundleURL)
这篇关于如何在 Xcode 中向 UIWebView 添加外部样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Xcode 中向 UIWebView 添加外部样式表?
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
