2

インラインスタイルを作成するとレンダリングされますが、xCode内の同じグループとディレクトリにあるcssファイルを参照しようとすると、レンダリングされません。

これが私がすることです:

<html>
<head>
    <link href="main.css" rel="stylesheet" type="text/css" /> 
</head>

<body>

    <h1>Hello, world! Learn</h1>

    <p>
    <a href="myscheme://advertising" class="buttonStyle">Click me!</a>
    </p> 
</body>
</html>

そして、次のようなmain.cssというcssファイルがあります。

body {background-color:#b0c4de;}
p {background-color:#e0ffff;}

なぜこれがCSSをレンダリングしないのか誰かが知っていますか?HTMLヘッダーから重要なものが欠落していますか?

そもそもHTMLを呼び出す方法は次のとおりです。

- (void)viewDidAppear:(BOOL)animated
{  
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"learn" 
                                                         ofType:@"html"];

    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [theWebView loadHTMLString:htmlString baseURL:nil];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:htmlFile]];

    [theWebView loadRequest:request];
}
4

1 に答える 1

8

問題は

[theWebView loadHTMLString:htmlString baseURL:nil];

baseURLはnilであるため、Webビューにはcssファイルがどこにあるかを判別する方法がありません。手動でデータを作成してロードする代わりに、HTMLファイルのパスをUIWebViewに直接渡します。

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"learn" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *rq = [NSURLRequest requestWithURL:url];
[webView loadRequest:rq];
于 2012-08-14T14:43:55.527 に答える