简体   繁体   English

更改字体大小时调整UIWebview框架的大小

[英]Resizing UIWebview's frame when changing its font size

I have a UIWebview I'd like to change the font size. 我有一个UIWebview我想更改字体大小。 To do that, I simply use javascript : 为此,我只使用javascript:

NSString *jsString = [NSString stringWithFormat:@"document.body.style.webkitTextSizeAdjust= '%d%%'",50];
    [t_webview stringByEvaluatingJavaScriptFromString:jsString];

The problem is that when I set the font size to 50%, there is a big blank area below the content of the uiwebview. 问题是,当我将字体大小设置为50%时,uiwebview的内容下方有一个很大的空白区域。

I tried to resize my webview to the height that fit the actual content size by running the following js but it always returns the height of the 100% text, not the 50% and actual one: 我试图通过运行以下js将Webview调整为适合实际内容大小的高度,但它始终返回100%文本的高度,而不是50%实际的高度:

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

---- logs ------ ----日志------

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

I also tried to use document.documentElement.scrollHeight instead of document.body.offsetHeight but this doesn't work either. 我也尝试使用document.documentElement.scrollHeight而不是document.body.offsetHeight,但这也不起作用。

Can anyone help me? 谁能帮我? Thanks. 谢谢。

edit: Here is how I load the html to the webview: 编辑:这是我将html加载到webview的方法:

NSString* t_html = [NSString stringWithFormat:@"<html><body>%@</body></html>",t_htmlPharmaco];
    [webview loadHTMLString:t_html baseURL:[[NSBundle mainBundle] bundleURL]];

Did you try to change the UIWebView scrollerView contentsize instead changing the body size? 您是否尝试过更改UIWebView scrollerView contentsize而不是更改主体大小?

Is something like this 是这样的吗

 t_webview.scrollView.contentSize=CGSizeMake(width, height); 

I am not sure if this applies to UIWebView. 我不确定这是否适用于UIWebView。 But in iOS, all controls (as far as i know) gets their height property set before setting the content. 但是在iOS中,据我所知,所有控件在设置内容之前都要先设置其height属性。

That being said, I would try to resize the font before setting the content inside the webview OR I would render the webview as how you are doing it and then when the webview gets properly created, then get the instance of it to resize the height. 话虽这么说,我会先尝试调整字体大小,然后再在Webview内设置内容,或者按照您的操作方式渲染WebView,然后在正确创建WebView时渲染它,然后获取其实例以调整高度。

Also another question arise if whether you want to keep the size of the UIWebView the same while changing the content size within? 如果在更改内容大小时是否要保持UIWebView的大小不变,还会出现另一个问题? (If so, use the answer by @jcesar). (如果是这样,请使用@jcesar的答案)。 However, if you are looking to change the size of the UIWebView then you will need to change the webview size 但是,如果您要更改UIWebView的大小,则需要更改webview的大小

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

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