簡體   English   中英

如何獲取當前Webview的內容大小?

[英]How can i get the current content size of webview?

當我在uiwebview中顯示epub時,我可以看到有480 px以下的行,因此如何將內容大小的高度限制為450 px並添加50px的底邊距。目前,iam使用以下代碼添加了總填充量,我不知道如何限制uiwebview的內容高度?

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

請幫我

我嘗試使用此代碼獲取內容大小,但返回的值為14783

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

嘗試在...上使用

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];
        }
    }
}

試試這個代碼:

- (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])];
         }
}

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM