Nick Emmerson
Nick Emmerson

Reputation: 13

Custom CSS in UIWebView

So in my iPad app, i'm hoping to add a custom Twitter feed using UIWebView but wanted to use a custom CSS file

Is this possible?

if so how would i go about do it?

Cheers

Upvotes: 1

Views: 887

Answers (2)

wottle
wottle

Reputation: 13619

You can get the DOMDocument from the webview,, and then add the css to it that way.

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

Upvotes: 0

Matt H
Matt H

Reputation: 6532

You can forcefully inject javascript into a UIWebView at any time, modifying the underlying HTML. You can either use basic javascript or, if a library like jQuery is loaded, use it:

[webView stringByEvaluatingJavaScriptFromString:@"$('.className').css('color','#f00')"];

You can also load entire scripts and modify, add, delete tags -- you just have to write the javascript to accomplish the changes.

Upvotes: 2

Related Questions