【问题标题】:How can I add an external stylesheet to a UIWebView in Xcode?如何在 Xcode 中向 UIWebView 添加外部样式表?
【发布时间】:2011-06-30 12:01:04
【问题描述】:

我查看了各种类似的问题和答案,但仍然无法解决这个问题,所以我添加了我自己的问题:

我正在玩 UIWebView。我可以使用内联 CSS 创建一个工作 html 页面。如果 CSS 在应用资源组的文件中,我无法弄清楚如何加载它。

我的代码是:

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 调用是这样的:

<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>

如果有任何想法或解决方案,我将不胜感激。非常感谢!

【问题讨论】:

  • 我对此不确定,所以我将其添加为评论,但我不认为“/greek123.css”是正确的。前导 / 表示这是一个绝对路径,而它应该是相对路径。
  • 另外,您不需要文件句柄来将文件内容放入 NSString。这是 NSString 上类似于 stringWithContentsOfFile 的方法:
  • 谢谢达伦,前面的斜线是一个实验,但没有奏效。感谢您提供有关使用 stringWithContentsofFile 的提示。很高兴知道。

标签: iphone html css uiwebview


【解决方案1】:

将 UIWebView 的 baseURL 更改为 mainbundle 的 url。

NSURL *mainBundleURL = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:mainBundleURL];

斯威夫特 3:

webView.loadHTMLString(contentString, baseURL: Bundle.main.bundleURL)

【讨论】:

  • 非常感谢。这正是我所需要的。我真的很感激。
  • 我有一个后续问题。如果我应该将其作为一个单独的主题,请告诉我...使用与外部文件中相同的内联 CSS,我得到的结果非常不同。为什么会这样?
  • 对不起,我对html和css几乎一无所知。所以我真的帮不上忙。
  • 这是对这个问题最好最简洁的回答。刚刚为我解决了问题。
  • 这个应该强调一下。我见过很多丑陋的变通方法,这可以干净地解决。
【解决方案2】:

我想我已经明白了。 htmlString 只是一个 NSString,我们可以替换其中的文本。这段代码对我有用

NSString *info = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"your html file name" ofType:@"html"] encoding: NSUTF8StringEncoding error: &error];

info=[info stringByReplacingOccurrencesOfString:@"styles.css" withString:@"stylesIPad.css"];

【讨论】:

  • 看起来不错..我会尽快尝试..大卫(OP)
【解决方案3】:
NSURL *BundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"name your style" ofType:@"css"]];
webView.opaque = NO;
webView.backgroundColor=[UIColor clearColor];
[webView loadHTMLString:htmlString baseURL:BundleURL];

【讨论】:

  • 或者你可以这样:newhmlString=[oldhtmlString stringByReplacingOccurrencesOfString:@"
【解决方案4】:

我认为我应该发布整个代码块以访问外部 CSS 文件。希望它对其他人有用:

- (void)viewDidLoad
{
    NSURL *mainBundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"mypagename" 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:mainBundleURL];

    [htmlString release];
}

【讨论】:

    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 2012-03-07
    • 2011-11-08
    • 2021-04-11
    • 1970-01-01
    • 2017-01-06
    • 2012-08-01
    • 1970-01-01
    相关资源
    最近更新 更多