Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript UIWebView更改行距文本_Javascript_Xcode_Ios5_Uiwebview - Fatal编程技术网

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

我正在寻找一种javascript方法或一种在UIWebView中更改文本行距的方法

有什么想法吗

编辑:

埃文已经回答了上面的问题,但我有另一个关于同一主题的问题


如何查询当前行距值的源代码?

您需要通过调用
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
等元素,它可能会有所不同。