Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView load css on the fly

I have the following code which loads and html file into a webview

- (void)awakeFromNib{

    NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
    NSString *htmlPath = [resourcesPath stringByAppendingString:@"/main.html"];
    [[self mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];

}

How would i dynamically load a css file (in the most efficient manner) as it does not suit to have the css file link in the html file

like image 349
ADAM Avatar asked Mar 19 '10 06:03

ADAM


1 Answers

You should access the DOM using the Objective-C DOM API and insert the appropriate <link> or <style> element into the DOM.

DOMDocument* domDocument=[webView mainFrameDocument];
DOMElement* styleElement=[domDocument createElement:@"style"];
[styleElement setAttribute:@"type" value:@"text/css"];
DOMText* cssText=[domDocument createTextNode:@"body{font-weight:bold;}"];
[styleElement appendChild:cssText];
DOMElement* headElement=(DOMElement*)[[domDocument getElementsByTagName:@"head"] item:0];
[headElement appendChild:styleElement];
like image 124
Rob Keniger Avatar answered Nov 03 '22 17:11

Rob Keniger