如何获取webview的当前内容大小?

时间:2013-06-03 04:47:26

标签: javascript ios objective-c uiwebview margin

当我在uiwebview中显示epub时,我可以看到有480 px以下的行,所以如何将内容大小限制为450像素并添加50px的底部边距。目前我添加了总填充量使用以下代码我不知道如何限制uiwebview的内容高度?

NSString *padding = @"document.body.style.padding='15px 15px 15px 15px';";
[webView stringByEvaluatingJavaScriptFromString:padding];

请帮帮我

我尝试使用此代码获取内容大小,但返回的值为14783

webView.scrollView.ContentSize.height;然后我得到14783

2 个答案:

答案 0 :(得分:1)

尝试在...上使用它

CGSize contentSize = CGSizeMake([[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"] floatValue],
                                [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]);

编辑.......

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    CGRect frame = webView.frame;

    frame.size.height = 5.0f;

    webView.frame = frame;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGSize mWebViewTextSize = [webView sizeThatFits:CGSizeMake(1.0f, 1.0f)];  // Pass about any size

    CGRect mWebViewFrame = webView.frame;


    mWebViewFrame.size.height = mWebViewTextSize.height;

    webView.frame = mWebViewFrame;


    //Disable bouncing in webview
    for (id subview in webView.subviews)
    {
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        {
            [subview setBounces:NO];
        }
    }
}

答案 1 :(得分:0)

试试这段代码:

- (void)webViewDidFinishLoad:(UIWebView *)webView1
{
      NSString  *totalHeight;

        totalHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];

         if([totalHeight intValue] >= 450)
         {
            [webView setFrame:CGRectMake(0, 0, webView.frame.size.width, 450)];
         }
         else
         {
          [webView setFrame:CGRectMake(0, 0, webView.frame.size.width, [totalHeight floatValue])];
         }
}

我希望它可以帮到你。