0

I have some HTML strings and a remote CSS link. I want to load the HTML strings and add the CSS to it in my webView. How can I do it? Thanks!

[_webView loadHTMLString:htmlString baseURL:someurl];

1 Answer 1

1

You could either

  1. fetch the CSS over the network (with e.g. NSURLConnection & friends)

  2. load it with loadHTMLString: enclosed in <style>[...]</style> tags (e.g. [NSString stringWithFormat:@"<style>%@</style>", loadedCSSString])

or

use Javascript (stringByEvaluatingJavaScriptFromString:) inside the webview, create and insert a link tag:

var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('href', '<your remote CSS link>');
document.head.appendChild(link);

The HTML must of course already be loaded when you do this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.