Javascript UIWebView更改行距文本
我正在寻找一种javascript方法或一种在UIWebView中更改文本行距的方法 有什么想法吗 编辑: 埃文已经回答了上面的问题,但我有另一个关于同一主题的问题Javascript UIWebView更改行距文本,javascript,xcode,ios5,uiwebview,Javascript,Xcode,Ios5,Uiwebview,我正在寻找一种javascript方法或一种在UIWebView中更改文本行距的方法 有什么想法吗 编辑: 埃文已经回答了上面的问题,但我有另一个关于同一主题的问题 如何查询当前行距值的源代码?您需要通过调用stringByEvaluatingJavaScriptFromString:将javascript注入UIWebView的源代码中 Javascript: var head = document.getElementsByTagName('head')[0], style = do
如何查询当前行距值的源代码?您需要通过调用
stringByEvaluatingJavaScriptFromString:
将javascript注入UIWebView
的源代码中
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);
var element = document.getElementsByTagName('h1')[0],
style = window.getComputedStyle(element),
lh = style.getPropertyValue('line-height');
return lh;
目标-C(可能在webViewDidFinishLoading:
中):
要检索页面上第一个
h1
元素的行高
值:
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);
var element = document.getElementsByTagName('h1')[0],
style = window.getComputedStyle(element),
lh = style.getPropertyValue('line-height');
return lh;
目标C:
NSString *js = /* The javascript above in a string */
NSString *lineHeight = [webView stringByEvaluatingJavaScriptFromString:js];
源文件来自哪里(web服务器、本地文件)?你能修改它吗?这是一个浏览器类型的应用程序。这一切都来自互联网,我想在内容加载到UIWebView后更改文本的行距。只需在这一行“行高:20px!重要;”-20px可以用变量替换,对吗?这是什么意思!重要的意思?是的,“20px”只是我选择的一个值。您可以使用
[NSString stringWithFormat:…]
将其插入字符串中。“!important”用于覆盖页面上已存在的其他行高
样式。还有一个问题,您如何从源中获取当前行高?这取决于应用该行高的元素。对于不同的p
,h1
等元素,它可能会有所不同。