UIWebView change line spacing text

半世苍凉 提交于 2019-12-12 05:05:40

问题


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 topic.

How can I query the source for the current line spacing value?


回答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];


来源:https://stackoverflow.com/questions/10775527/uiwebview-change-line-spacing-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!