2011-12-19 15 views
3

ダウンロードしたxhtmlファイルにローカルのCSSファイルを「挿入」しようとしています。UIWebViewにローカルCSSを挿入する

私はそれを行う方法の多くの例を見つけたが、それはちょうど私のために動作しません...ここで

が私のコードです:私はここで間違って

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSString *cssPath = [path stringByAppendingPathComponent:@"test.css"]; 

    NSString *js = [NSString stringWithFormat:@"var cssNode = document.createElement('link');" 
       "cssNode.type = 'text/css';" 
       "cssNode.rel = 'stylesheet';" 
       "cssNode.href = '%@';", cssPath]; 
    js = [NSString stringWithFormat:@"%@document.getElementsByTagName('head')[0].appendChild(cssNode);", js]; 
    //m_webview is a member 
    [m_webView stringByEvaluatingJavaScriptFromString:js]; 
} 

何をしているのですか?

ありがとう、

答えて

7

私もこれをやっていませんでした。

サーバーからHTMLファイルを読み込み、ローカルCSSファイルを使用してスタイリングを変更しようとしました。

は、まず私が

[webView loadRequest:reqObj]; 

を使用し、それが- (void)webViewDidFinishLoad:(UIWebView *)webViewを打つとき、私は子供に「頭」としてCSSファイルをプッシュしようとしていた。「それはdidnの...

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSString *cssPath = [path stringByAppendingPathComponent:@"style.css"]; 

NSString *js = [NSString stringWithFormat:@"var cssChild = document.createElement('link');" 
       "cssChild = 'text/css';" 
       "cssChild = 'stylesheet';" 
       "cssChild = '%@';", cssPath]; 
js = [NSString stringWithFormat:@"%@ document.getElementsByTagName('head')[0].appendChild(cssChild);", js]; 
[webView stringByEvaluatingJavaScriptFromString:js]; 
} 

ので、トンの仕事...

はその後、私は

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:path];  
[webView loadHTMLString:htmlString baseURL:baseURL]; 
を試してみました

(私はhtmlStringにHTML文字列をコピーしました)、- (void)webViewDidFinishLoad:(UIWebView *)webViewの中に上記のコードのようにCSSを挿入しました。そしてそれは働いた!

しかし...私のHTMLファイルは、リモート・サーバに格納されており、私はHTML文字列を持っていなかったので、私はHTMLを取得するには

NSString* myFile = [NSString stringWithFormat:@"http://blablabla.com/file.html"]; 
NSString* myFileURLString = [myFile stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
NSData *myFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myFileURLString]]; 
NSString* myFileHtml = [[[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding] autorelease]; 

を使用。今、私は 'myFileHtml'の中に生のHTMLテキストを持っています。 私は今

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:path];  
[webView loadHTMLString:myFileHtml baseURL:baseURL]; 

そして、それに私のCSSファイルを注入し、「webViewDidFinishLoad」で応答をキャッチ使用し、それが働いた:)

たぶん、この問題への別の、よりエレガントな、解決策はありますが、これはあります私が思いついたのは...

希望しました。

+0

ありがとう! それはうまくいった。 「[myFileData release];」を追加するだけでした。 – Gal

0

多分これはトリックを行う必要がありますか? (Xcodeでテストされていません):

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 

    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSString *cssPath = [path stringByAppendingPathComponent:@"test.css"]; 

    NSString *js = [NSString stringWithFormat:@"var cssNode = document.createElement('link');" 
        "cssNode.type = 'text/css';" 
        "cssNode.rel = 'stylesheet';" 
        "cssNode.href = '%@';", cssPath]; 
    js = [NSString stringWithFormat:@"%@document.getElementsByTagName('head')[0].appendChild(cssNode);", js]; 


    NSString *k = [NSString stringWithFormat:@"var script = %@; %@", cssPath, js]; 

    //m_webview is a member 
    [m_webView stringByEvaluatingJavaScriptFromString:k]; 
} 
+0

ありがとうございますが、それでも同じように見えます。私は 'stringByEvaluatingJavaScriptFromString'が動作したか失敗したかをどうやって確認できますか? – Gal

2

cssNode.href = '%の@';」、cssPath];それはウェブサーバからファイルを取得しようとするため、上記の

ラインは失敗のようなものを使用する:

cssNode.href = 'ファイル://%@'; "、cssPath];

は、ローカルファイルシステムにアクセスします。 Safariの開発者モードを使用して、iPhoneシミュレータを調べ、ファイルが見つかって読み込まれているかどうかを確認できます。


結局、スタイルシートにリンクする代わりにインラインスタイルシートを実装しました。ここでは、私のために動作するコードです

1

私は同じ質問への答えを探しながらこれに遭遇しました。 WebサーバーからHTMLページをUIWebViewにロードしています。 HTMLには既にCSSがたくさんあり、ウェブ閲覧に適しています。私の場合、私は表示したかった:何も無関係のdiv。

HTMLをローカルに保存してファイルからロードしたくないので、概念的には他のものと概念的に似ていますが、私の好みに合わせて別の解決策を思いつきました。これはSwift 3です。ページが読み込まれた後、表示される前に実行してください:

let css = "#someDiv {display:none;} #otherDiv {display:none;} .someClass {display:none;}" 
let js = "var sheet = document.createElement('style');sheet.innerHTML = \"\(css)\";document.body.appendChild(sheet);" 
webView.stringByEvaluatingJavaScript(from: js) 
関連する問題