How to change the color and font size UIWebview

2019-06-25 04:05发布

问题:

In my application I am loading HTML String which is parsed from the json file the string already contain font color and font size but I need to change the color and size of the font. To replace that I have already use the followings code but its not working

NSString *webString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", 
                          textFontSize];
    [web stringByEvaluatingJavaScriptFromString:webString];

Is there is any other way to change it.If yes means please suggest me the answer.

回答1:

try this,

[webView  loadHTMLString:[NSString stringWithFormat:@"<div id ='foo' align='justify' style='font-size:14px; font-family:helvetica; color:#ffffff';>%@<div>",yourString] baseURL:nil];

it may helps you....



回答2:

You can try this java script in to your webview's delegate webViewDidFinishLoad method

NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];

NSString *changeColor = [NSString stringWithFormat:@"addCSSRule('html, body, div, p, span, a', 'color: #000000;')"];

[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
[webView stringByEvaluatingJavaScriptFromString:changeColor];

You can do so many changes using java script in webview's did finish load method.

Let me know if you still able to not solve