フォントサイズを変更したいUIWebviewがあります。これを行うには、単純に javascript を使用します。
NSString *jsString = [NSString stringWithFormat:@"document.body.style.webkitTextSizeAdjust= '%d%%'",50];
[t_webview stringByEvaluatingJavaScriptFromString:jsString];
問題は、フォント サイズを 50% に設定すると、uiwebview のコンテンツの下に大きな空白領域ができることです。
次の js を実行して、実際のコンテンツ サイズに合わせて Web ビューのサイズを変更しようとしましたが、50% の実際のテキストではなく、常に 100% のテキストの高さを返します。
NSLog(@"font size: %i%%",theHeightIWant);
NSString* t_height = [t_webview stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
NSLog(@"height before: %@",t_height);
NSString *jsString = [NSString stringWithFormat:@"document.body.style.webkitTextSizeAdjust= '%d%%'",theHeightIWant];
[t_webview stringByEvaluatingJavaScriptFromString:jsString];
t_height = [t_webview stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
NSLog(@"height after: %@",t_height);
---- ログ ------
font size: 95%
height before: 932
height after: 932
font size: 90%
height before: 932
height after: 932
font size: 85%
height before: 932
height after: 932
また、document.body.offsetHeight の代わりに document.documentElement.scrollHeight を使用しようとしましたが、これも機能しません。
誰でも私を助けることができますか?ありがとう。
編集:これは、htmlをwebviewにロードする方法です:
NSString* t_html = [NSString stringWithFormat:@"<html><body>%@</body></html>",t_htmlPharmaco];
[webview loadHTMLString:t_html baseURL:[[NSBundle mainBundle] bundleURL]];