Iphone 如何根据HTML文本动态改变webview的高度

Iphone 如何根据HTML文本动态改变webview的高度,iphone,uitableview,uiwebview,Iphone,Uitableview,Uiwebview,我想根据Html文本增加webview的大小,行,但我的web视图大小不正确,最初我是在固定框架中显示内容,后来当用户选择行时,我需要将单元格扩展为Html文本,请帮助我解决这个问题,我使用的代码是 enter code here NSLog(@"web view and selected ==== %d ---%d",webView.tag,selectedRow); //CGFloat webViewHeight = 0.0f; if (webView.subviews.count >

我想根据Html文本增加webview的大小,行,但我的web视图大小不正确,最初我是在固定框架中显示内容,后来当用户选择行时,我需要将单元格扩展为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);
    }

    }

要获取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文档完全呈现(即,您的代表收到
webviewdiffinishload:

注:如果你想快速检查,你也可以试试:

    NSUInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] intValue];
这也应该有效,但上面的版本更便于移植