简体   繁体   English

UIWebView更改行间距文本

[英]UIWebView change line spacing text

I'm looking for a javascript method or a way to change line spacing of text in a UIWebView. 我正在寻找一种javascript方法或一种在UIWebView中更改文本行间距的方法。

Any ideas? 有任何想法吗?

Edit: 编辑:

Evan has answered the above question, but I have another on the same topic. Evan已回答了上述问题,但我对同一主题提出了另一个问题。

How can I query the source for the current line spacing value? 如何查询当前行间距值的来源?

You will need to inject javascript into the UIWebView 's source by calling stringByEvaluatingJavaScriptFromString: . 您需要通过调用stringByEvaluatingJavaScriptFromString:将javascript注入UIWebView的源代码中。

Javascript: 使用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: ): Objective-C(可能在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: 要检索页面上第一个h1元素的line-height值:

Javascript: 使用Javascript:

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

Objective-C: Objective-C的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM