Answer the question
In order to leave comments, you need to log in
How to add custom css inside webview?
The bottom line is this: there is a WebView into which a string containing HTML is passed.
Looks like that:
let newsHTML = receviedNewsText
viewNewsHTML.loadHTMLString(newsHTML, baseURL: nil)
viewNewsHTML
- actually, himself WebView
; receviedNewsText
is HTML (from the tags there are only p and sometimes a), which is obtained from the server via JSON. Answer the question
In order to leave comments, you need to log in
The solution was found (albeit in ObjC, but it was not difficult to rewrite it in Swift):
- (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
}
You can create your own .css file and, when loading html, convert it to a string and add it to the beginning of the line
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question