Skip to content Skip to sidebar Skip to footer

UIWebView Change Line Spacing Text

I'm looking for a javascript method or a way to change line spacing of text in a UIWebView. Any ideas? Edit: Evan has answered the above question, but I have another on the same to

Solution 1:

You will need to inject javascript into the UIWebView's source by calling stringByEvaluatingJavaScriptFromString:.

Javascript:

var head = document.getElementsByTagName('head')[0],
    style = document.createElement('style'),
    rules = document.createTextNode('* { line-height: 20px !important; }');
style.type = 'text/css';
if(style.styleSheet)
    style.styleSheet.cssText = rules.nodeValue;
else style.appendChild(rules);
head.appendChild(style);

Objective-C (probably in webViewDidFinishLoading:):

NSString *js = /* The javascript above in a string */
[webView stringByEvaluatingJavaScriptFromString:js];

To retrieve the line-height value of the first h1 element on the page:

Javascript:

var element = document.getElementsByTagName('h1')[0],
    style = window.getComputedStyle(element),
    lh = style.getPropertyValue('line-height');
return lh;

Objective-C:

NSString *js = /* The javascript above in a string */
NSString *lineHeight = [webView stringByEvaluatingJavaScriptFromString:js];

Post a Comment for "UIWebView Change Line Spacing Text"