在 UIWebView 中使用本地 CSS 文件

发布于 2025-01-07 19:14:26 字数 2301 浏览 0 评论 0原文

我已经在网上搜索并尝试了几种方法,但我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情 2025-01-14 19:14:26

必须在文件夹引用中包含文件的相对路径

您的 css 文件是否位于应用程序的根目录?如果是这样,我不知道为什么你的代码不起作用。我的猜测是,您的 css 文件存在于“文件夹引用”(蓝色文件夹)内,在这种情况下,您还必须在 baseURL 中包含文件夹路径(或将 html 更改为 href=\" someFolder/style.css\")。

请注意,当您编译应用程序时,文件夹组内的资源(图像等...)将被平铺到同一文件夹中,但文件夹引用保持其文件夹层次结构。

仔细检查文件位置

  1. 找到您的应用程序(MyCoolProj.app 文件)。您可以通过展开“Products”从 XCode 中执行此操作。下面应该是您的 .app 文件。如果您打开该文件的上下文菜单,您应该能够“在 Finder 中显示”。
  2. 现在您在查找器中找到了 .app 文件,选择它并再次打开上下文菜单。现在选择“显示包内容”。
  3. 您现在应该可以看到应用程序内的所有文件。找到你的 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

  1. Find your app (MyCoolProj.app file). You can do this from XCode by expanding "Products". Under this should be your .app file. If you bring up a context menu for this file, you should be able to "Show in Finder".
  2. Now that you found your .app file in finder, select it and again bring up the context menu. Now select "Show Package Contents".
  3. You should now see all files inside your app. Find where your css file is. If it is inside a folder, then it is definitely in a folder reference in your project and to reference it you MUST include the relative folder path you traversed to get to it.

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.

白芷 2025-01-14 19:14:26

您可以使用样式标签将样式直接实现到 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!!

紫竹語嫣☆ 2025-01-14 19:14:26

您还可以创建 html 文件并将其加载到应用程序内的 UIWebview 中,如下所示:

NSString *resources = [[NSBundle mainBundle] resourcePath]; 
NSString *filePath = [resources stringByAppendingPathComponent:@"infotext.html"];

NSError *error = nil;
NSString * resourceFileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

if (error != nil) {
    @throw [NSException exceptionWithName:[error domain] reason:[error localizedDescription] userInfo:[error userInfo]];
}

NSURL *baseURL = [NSURL fileURLWithPath:resources];
[self.mWebView loadHTMLString:resourceFileContent baseURL:baseURL];

Also you can create the html file and load it in the UIWebview inside the app, something like this:

NSString *resources = [[NSBundle mainBundle] resourcePath]; 
NSString *filePath = [resources stringByAppendingPathComponent:@"infotext.html"];

NSError *error = nil;
NSString * resourceFileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

if (error != nil) {
    @throw [NSException exceptionWithName:[error domain] reason:[error localizedDescription] userInfo:[error userInfo]];
}

NSURL *baseURL = [NSURL fileURLWithPath:resources];
[self.mWebView loadHTMLString:resourceFileContent baseURL:baseURL];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文