UIWebView サブクラスの stringByEvaluatingJavaScriptFromString: に渡す 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);
これを stringByEvaluatingJavaScriptFromString: メソッドに追加しました。ピクセル値を指定する限り、コードは機能します。以下のコードのような変数を使用しても、何もしません。私は何を間違っていますか?
間隔を増やす必要があるか減らす必要があるかを伝えるために、 largeBool を使用しています。次に、最大値と最小値を設定し、行の高さを変数として使用しています。
- (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];
}
また、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);
}
ログに「0」しか表示されません。
前もって感謝します!