问题描述
我正在寻找一种javascript方法或一种在UIWebView中更改文本行间距的方法。
I'm looking for a javascript method or a way to change line spacing of text in a UIWebView.
任何想法?
编辑:
Evan回答了上述问题,但我对同一主题提出了另一个问题。
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?
推荐答案
您需要将javascript注入通过调用 stringByEvaluatingJavaScriptFromString:
来获取 UIWebView
的来源。
You will need to inject javascript into the UIWebView
's source by calling stringByEvaluatingJavaScriptFromString:
.
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(可能在 webViewDidFinishLoading:
):
Objective-C (probably in webViewDidFinishLoading:
):
NSString *js = /* The javascript above in a string */
[webView stringByEvaluatingJavaScriptFromString:js];
检索行-height
页面上第一个 h1
元素的值:
To retrieve the line-height
value of the first h1
element on the page:
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];
这篇关于UIWebView更改行间距文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!