The essence of this: there is a WebView , which is passed a string containing HTML.
Looks like that:

 let newsHTML = receviedNewsText viewNewsHTML.loadHTMLString(newsHTML, baseURL: nil) 


viewNewsHTML - actually, the WebView itself;
receviedNewsText is HTML (from tags there is only p and sometimes a), which comes from the server via JSON.
Since there are no styles there, the text is plain, small, and ugly.

The goal is that inside WebView I can use styles for the p tag.

How can I do that? Ideally, this will be a .css file inside the application, the styles from which will be loaded into WebView. But maybe there is some other way.

I would appreciate advice. Thank!

    1 answer 1

    Taken from here

    Here is a sample code to increase the font size using css

     - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *cssString = @"body { font-family: Helvetica; font-size: 50px }"; // 1 NSString *javascriptString = @"var style = document.createElement('style'); style.innerHTML = '%@'; document.head.appendChild(style)"; // 2 NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString]; // 3 [webView stringByEvaluatingJavaScriptFromString:javascriptWithCSSString]; // 4 } 

    Swift

     func webViewDidFinishLoad(webView: UIWebView) { let cssString = "body { font-family: Helvetica; font-size: 50px }" let javascriptString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style)" webView.stringByEvaluatingJavaScriptFromString(javascriptString) } 
    • It would be nice to have it on Swift. :) But my skills were enough to rewrite it. :) - Roman Ekimov
    • added in reply - Max Mikheyenko