Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在Xcode中将外部样式表添加到UIWebView?_Iphone_Html_Css_Uiwebview - Fatal编程技术网

Iphone 如何在Xcode中将外部样式表添加到UIWebView?

Iphone 如何在Xcode中将外部样式表添加到UIWebView?,iphone,html,css,uiwebview,Iphone,Html,Css,Uiwebview,我已经看过了各种类似的问题和答案,但仍然无法实现这一点,因此我添加了我自己的问题: 我在玩UIWebView。我可以用内联CSS创建一个工作html页面。如果CSS位于app resources组的文件中,我无法确定如何加载它 我的代码是: NSString *path = [[NSBundle mainBundle] pathForResource:@"webViewPage2" ofType:@"html"]; NSFileHandle *readHandle = [NSFileHa

我已经看过了各种类似的问题和答案,但仍然无法实现这一点,因此我添加了我自己的问题:

我在玩UIWebView。我可以用内联CSS创建一个工作html页面。如果CSS位于app resources组的文件中,我无法确定如何加载它

我的代码是:

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>

一些头衔
我们可以把说明放在这里。例如:
“哦,你不该按那个按钮!”


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

将UIWebView的baseURL更改为mainbundle的url

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

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

我想我应该发布整个代码块来访问外部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];
}

我想我已经弄明白了。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"];

我不确定这一点,所以我将添加这一点作为评论,但我不认为“/greek123.css”是正确的。前导/表示这是一个绝对路径,而它应该是相对路径。此外,将文件内容转换为NSString不需要文件句柄。这是一个关于NSString的方法,类似于stringWithContentsOfFile:谢谢Darren,前面的斜杠是一个实验,但不起作用。感谢您提供有关使用stringWithContentsofFile的提示。很高兴知道。谢谢你。这正是我需要的。我真的很感激。我有一个后续问题。。让我知道我是否应该把它作为一个单独的主题。。。使用与外部文件中相同的CSS内联,我得到了非常不同的结果。为什么会这样?对不起,我对html和css几乎一无所知。所以我实在帮不上忙。这是对这个问题最好、最简洁的回答。刚刚为我解决了这个问题。这应该被强调。我已经看到了很多丑陋的解决方法,这很好地解决了问题。或者你可以这样做:newhmlString=[oldhtmlString stringByReplacingOccurrencesOfString:@“with string:@”\n@import url(\“your name css.css\”);\n“];在添加上一个字符串后结束:))看起来很好。。我会尽快试一试。。大卫(作品)
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"];