У меня есть метод javascript для передачи в stringByEvaluatingJavaScriptFromString: в подклассе UIWebView.UIWebView изменение межстрочного интервала для текста в источнике
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);
Я добавил его к stringByEvaluatingJavaScriptFromString: метод и до тех пор, как я определяю значение пикселя, код работает. Если я использую переменную, подобную приведенному ниже коду, она ничего не делает. Что я делаю не так?
Я использую largeBool, чтобы сообщить мне, нужно ли увеличить или уменьшить интервал. Затем я устанавливаю 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];
}
Я также использую метод didFinishLoading делегата от UIWebView, чтобы сохранить текущее значение межстрочного интервала.
- (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);
}
Все, что я получаю, это «0» в журнале.
Заранее благодарен!