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

时间:2012-08-28 13:05:41

标签: javascript ios uiwebview resize

我有一个UIWebview我想改变字体大小。 为此,我只使用javascript:

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

问题在于,当我将字体大小设置为50%时,uiwebview的内容下面会有一个很大的空白区域。

我尝试通过运行以下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

我还尝试使用document.documentElement.scrollHeight而不是document.body.offsetHeight,但这也不起作用。

任何人都可以帮助我吗?感谢。

编辑:以下是我将html加载到webview的方法:

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

2 个答案:

答案 0 :(得分:0)

您是否尝试更改UIWebView scrollerView内容而不是更改正文大小?

是这样的

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

答案 1 :(得分:0)

我不确定这是否适用于UIWebView。但是在iOS中,所有控件(据我所知)在设置内容之前都会设置它们的高度属性。

话虽这么说,我会尝试在设置webview中的内容之前调整字体大小,或者我会将webview呈现为你如何做,然后在正确创建webview时,然后获取它的实例来调整大小高度。

如果您想在更改内容大小的同时保持UIWebView的大小相同,还会出现另一个问题吗? (如果是这样,请使用@jcesar的答案)。但是,如果您要更改UIWebView的大小,则需要更改网页浏览量

相关问题