简体   繁体   English

UIWebView更改源中文本的行距

[英]UIWebView change line spacing for text in source

I have a javascript method to pass in to stringByEvaluatingJavaScriptFromString: in a UIWebView subclass. 我有一个JavaScript方法可传递给UIWebView子类中的stringByEvaluatingJavaScriptFromString:。

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);

I've added it to the stringByEvaluatingJavaScriptFromString: method and as long as I specify a pixel value, the code works. 我已经将其添加到stringByEvaluatingJavaScriptFromString:方法中,并且只要指定了像素值,代码就可以正常工作。 If I use a variable like the code below, it doesn't do anything. 如果我使用像下面的代码这样的变量,它不会执行任何操作。 What am I doing wrong? 我究竟做错了什么?

I'm using the largerBool to tell me if the spacing needs to increase or decrease. 我正在使用largeBool告诉我间距是否需要增加或减小。 Then I'm setting max and min, and using line height as the variable. 然后,我设置max和min,并使用行高作为变量。

   - (void)changeLineSpacingLarger:(BOOL)largerBool {

    if (largerBool == YES) { // A+
        lineHeight = (lineHeight < 100) ? lineHeight +10 : lineHeight;
    }

    else if (largerBool == NO) { // A-
        lineHeight = (lineHeight > 20) ? lineHeight -10 : lineHeight;
    }

    NSString *jsString = [[NSString alloc] initWithFormat:@"var head = document.getElementsByTagName('head')[0], style = document.createElement('style'), rules = document.createTextNode('* { line-height: '%d%%'px !important; }'); style.type = 'text/css'; if(style.styleSheet) style.styleSheet.cssText = rules.nodeValue; else style.appendChild(rules); head.appendChild(style);", lineHeight];
    [self stringByEvaluatingJavaScriptFromString:jsString];
}

I'm also using the didFinishLoading delegate method of UIWebView to save the current line spacing value. 我还使用UIWebView的didFinishLoading委托方法来保存当前的行距值。

- (void)updateLineSpacingValue {
    NSString *jsString = [[NSString alloc] initWithFormat:@"var element = document.getElementsByTagName('h1')[0], style = window.getComputedStyle(element), lh = style.getPropertyValue('line-height'); return lh;", textFontSize];
    NSString *stringValue = [self stringByEvaluatingJavaScriptFromString:jsString];

    lineHeight = [stringValue intValue];

    NSLog(@"Line Height: %i", lineHeight);
}

All I get is '0' in the Log. 我得到的只是日志中的“ 0”。

Thanks in advance! 提前致谢!

First, line-height: '%d%%'px should be line-height: '%d'px . 首先, line-height: '%d%%'px应该是line-height: '%d'px Otherwise, you will build something like line-height: '12%%'px , which won't be parsed when the source is rendered. 否则,您将构建类似于line-height: '12%%'px东西line-height: '12%%'px ,渲染源时将不会对其进行解析。


Second, stringValue will be something like "12px", so you cannot directly convert it to a number without removing "px". 其次, stringValue类似于“ 12px”,因此您不能在不删除“ px”的情况下将其直接转换为数字。

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

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