在 UIWebView 中使用本地 CSS 文件
我已经在网上搜索并尝试了几种方法,但我的 XCode 项目中有一个 CSS 文件和一个 UIWebView,它的控制器中写入了 html。
我需要让 WebView 识别 CSS 文件并在向用户显示视图时使用它。我不能依赖应用程序访问互联网,所以我尝试了我见过的所有不依赖于它的方法。以下是我到目前为止所拥有的内容,但它不起作用:
style.css
位于支持文件中
NSString *html = @"<html><head><link rel=\"stylesheet\" type=\"text/css\"
href=\"style.css\" ><title>Words</title><link rel=\"stylesheet\" type=\"text/css\"
href=\"style.css\" /></head><body><table cellpadding=\"0\" cellspacing=\"0\"
border=\"0\"
width=\"100%\" class=\"resource-table\"><tr><th width=\"20%\">Reference/Performance
Level</th><th width=\"20%\">Words</th><th width=\"20%\">Words</th><th
width=\"20%\">Words</th><th width=\"20%\">Highly Effective</th></tr><tr><td><p
class=\"bold\">Words</p><p>Words</p></td><td><ul class=\"normal\"><li>Words</li></ul>
</td><td><ul class=\"normal\"><li>Words</li></ul></td><td><ul class=\"normal\">
<li>Words</li></ul></td><td><ul class=\"normal\"><li>Words</li></ul></td></tr><tr><td>
<p class=\"bold\">Domain 4f P. 107</p><p>4-Decision Making</p></td><td><ul
class=\"normal\"><li>Words</li></ul></td><td><ul class=\"normal\"><li>Words</li></ul>
</td><td><ul class=\"normal\"><li>Words</li></ul></td><t<ul class=\"normal\">
<li>Words</li></ul></td></tr><tr><td><p class=\"bold\">Words</p></td><td></td><td>
</td><td></td><td><ul class=\"normal\"><liWords</li><li>Words</li></ul></td></tr>
</table></body></style></html>";
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:html baseURL:baseURL];
I have searched the web and tried this a couple ways now but I have a CSS file within my XCode project and a UIWebView which has the html written in it's controller.
I need to get the WebView to recognize the CSS file and use it when the view is shown to the user. I cannot depend on the application having access to internet so I have tried every method I have seen which does not depend on it. The following is what I have so far and it does not work:
style.css
is located in supporting files
NSString *html = @"<html><head><link rel=\"stylesheet\" type=\"text/css\"
href=\"style.css\" ><title>Words</title><link rel=\"stylesheet\" type=\"text/css\"
href=\"style.css\" /></head><body><table cellpadding=\"0\" cellspacing=\"0\"
border=\"0\"
width=\"100%\" class=\"resource-table\"><tr><th width=\"20%\">Reference/Performance
Level</th><th width=\"20%\">Words</th><th width=\"20%\">Words</th><th
width=\"20%\">Words</th><th width=\"20%\">Highly Effective</th></tr><tr><td><p
class=\"bold\">Words</p><p>Words</p></td><td><ul class=\"normal\"><li>Words</li></ul>
</td><td><ul class=\"normal\"><li>Words</li></ul></td><td><ul class=\"normal\">
<li>Words</li></ul></td><td><ul class=\"normal\"><li>Words</li></ul></td></tr><tr><td>
<p class=\"bold\">Domain 4f P. 107</p><p>4-Decision Making</p></td><td><ul
class=\"normal\"><li>Words</li></ul></td><td><ul class=\"normal\"><li>Words</li></ul>
</td><td><ul class=\"normal\"><li>Words</li></ul></td><t<ul class=\"normal\">
<li>Words</li></ul></td></tr><tr><td><p class=\"bold\">Words</p></td><td></td><td>
</td><td></td><td><ul class=\"normal\"><liWords</li><li>Words</li></ul></td></tr>
</table></body></style></html>";
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:html baseURL:baseURL];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
必须在文件夹引用中包含文件的相对路径
您的 css 文件是否位于应用程序的根目录?如果是这样,我不知道为什么你的代码不起作用。我的猜测是,您的 css 文件存在于“文件夹引用”(蓝色文件夹)内,在这种情况下,您还必须在 baseURL 中包含文件夹路径(或将 html 更改为
href=\" someFolder/style.css\"
)。请注意,当您编译应用程序时,文件夹组内的资源(图像等...)将被平铺到同一文件夹中,但文件夹引用保持其文件夹层次结构。
仔细检查文件位置
旁注:您还应该注意到,文件夹组(不是文件夹引用)中的所有图像和其他资源都存在于 .app 文件根部的同一文件夹中。他们不再没有文件夹层次结构。这就是为什么您可以使用
[UIImage imageNamed:]
查找仅包含其名称的图像,例如:[UIImage imageNamed:@"someImageName.png"]
。如果它们存在于文件夹引用中,则您必须再次包含该路径。Must Include Relative Path for Files in Folder References
Is your css file located at the root of the app? If so, I don't know why your code is not working. My guess is that your css file exists inside a "folder reference" (blue folder) in which case you must also include the folder path in the baseURL (or change html to
href=\"someFolder/style.css\"
).Note that resources (images, etc...) inside of folder groups will get flattened into the same folder when you compile your app, but folder references maintain their folder heirarchy.
Double Check File Location
Side Note: You should also notice that all images and other resources that were in folder groups (not folder references) all exist inside the same folder at the root of your .app file. They no longer have no folder hierarchy. This is why you can find images with only their name using
[UIImage imageNamed:]
like:[UIImage imageNamed:@"someImageName.png"]
. If they existed in a folder reference, again you would have to include that path.您可以使用样式标签将样式直接实现到 HTML 代码中,而不是使用单独的文件。
我希望它能帮助你!
You can implement the style directly into the HTML code, with an style tag, instead of a separated file.
I hope it helps you!!
您还可以创建 html 文件并将其加载到应用程序内的 UIWebview 中,如下所示:
Also you can create the html file and load it in the UIWebview inside the app, something like this: