2013-02-05 25 views
0

我想增加webview的大小,根據Html文本行,但我的網絡視圖大小不正確,內在我顯示的內容在固定幀,稍後時用戶選擇行我需要擴大電池accoriding到HTML文本,請幫我解決這個,這是我使用的代碼是如何動態chnage webview的高度根據HTML文本

enter code here NSLog(@"web view and selected ==== %d ---%d",webView.tag,selectedRow); 
//CGFloat webViewHeight = 0.0f; 
if (webView.subviews.count > 0) { 
    UIView *scrollerView = [webView.subviews objectAtIndex:0]; 
    if (scrollerView.subviews.count > 0) { 
     UIView *webDocView = scrollerView.subviews.lastObject; 
     if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]]) 
      webViewHeight = webDocView.frame.size.height; 

     NSLog(@"web view height is %d",webViewHeight); 
    } 

    } 

回答

0

獲得實際的HTML文件的高度,你應該做的:

NSUInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"getDocHeight()"] intValue]; 

其中getDocHeight是:

function getDocHeight() { 
    return Math.max(
    Math.max(document.body.scrollHeight, document.documentElement.scrollHeight), 
    Math.max(document.body.offsetHeight, document.documentElement.offsetHeight), 
    Math.max(document.body.clientHeight, document.documentElement.clientHeight) 
); 
} 

爲此,您必須等待HTML文檔完全呈現(即您的代表收到webViewDidFinishLoad:)。

P.S:如果你想要做一個快速檢查,你也可以簡單地嘗試:

NSUInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] intValue]; 

這也應該工作,但上面的版本更加輕便。

相關問題