2013-06-03 43 views
0

當我在uiwebview中顯示epub時,我可以看到有480行以下的行,所以我怎樣才能限制內容大小高度爲450像素,並添加50px底部邊距。目前IAM通過使用下面的代碼添加總填充我不知道如何限制uiwebview的內容高度?如何獲取當前webview的內容大小?

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

請幫我

我想這代碼用於獲取內容的大小,但它返回的1478​​3

webView.scrollView.ContentSize.height;一個值,然後我得到14783

回答

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

我可以通過使用浮點打印嗎? – Navi

+0

是的,你可以喜歡這個。 NSLog(@「width =%f,height =%f」,contentSize.width,contentSize.height); –

+0

我得到這個值29975963932020441088.000000作爲內容大小 – Navi

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

我希望它可以幫助你。

+0

請讓我知道它是否工作?謝謝 – chandan