UIWebView加载html图片自适应高度

创建webView

        web_info.scrollView.scrollEnabled =YES;
        web_info.delegate =self;
        
        web_info.scrollView.bounces =NO;
        web_info.scrollView.showsHorizontalScrollIndicator = NO;
        [web_infosizeToFit];
        
        NSString *htmlcontent = [NSStringstringWithFormat:@"<div id=\"webview_content_wrapper\">%@</div>", htmlText];
        [cell.web_infoloadHTMLString:htmlcontentbaseURL:nil];
1、
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取页面高度(像素)
    NSString * clientheight_str = [webViewstringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
    float clientheight = [clientheight_strfloatValue] /2;
    //设置到WebView上
    webView.frame =CGRectMake(0,0,ScreenWidth, clientheight);
    //获取WebView最佳尺寸(点)
    CGSize frame = [webViewsizeThatFits:webView.frame.size];
    //获取内容实际高度(像素)
    NSString * height_str= [webViewstringByEvaluatingJavaScriptFromString:@"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top'))  + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"];
    float height = [height_strfloatValue] /2;
    //内容实际高度(像素)*点和像素的比
    //height = height * frame.height / clientheight;
    
    offsetHeight = height * frame.height / clientheight;
    //再次设置WebView高度(点)
    //webView.frame = CGRectMake(0, 0, self.view.frame.size.width, height);
}

2、
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat webViewHeight=[webView.scrollView contentSize].height;
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
_webTablewView.contentSize = CGSizeMake(320, newFrame.size.height + 64 + KWIDTH - 100);
 
}


3、.执行js语句 直接获取html文档的dom高度
- (void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloatwebViewHeight =[[webViewstringByEvaluatingJavaScriptFromString:@document.body.offsetHeight]floatValue];
// CGFloat webViewHeight= [[webViewstringByEvaluatingJavaScriptFromString:@document.body.scrollHeight]floatValue];
CGRectnewFrame = webView.frame;
newFrame.size.height= webViewHeight;
webView.frame= newFrame;
}

4、先将UIWebView的高度设为最小,然后再使用sizeThatFits就会返回刚好合适的大小 (本人使用 ,可行)
-(void)webViewDidFinishLoad:(UIWebView*)webView{
//    CGSize actualSize = [webView sizeThatFits:CGSizeZero];
//    
//    CGRect newFrame = webView.frame;
//    
//    newFrame.size.height = actualSize.height;
//    
//    webView.frame = newFrame;
    //sizeThatFits方法有个问题,如果当前UIView的大小比刚好合适的大小还大,则返回当前的大小,不会返回最合适的大小值,所以使用 sizeThatFits前,先将UIWebView的高度设为最小,即1,然后再使用sizeThatFits就会返回刚好合适的大小。
    CGRect frame = webView.frame;
    frame.size.height =1;
    webView.frame = frame;
    CGSize fittingSize = [webViewsizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    offsetHeight = webView.frame.size.height;
}

5、遍历webview子视图 获取UIWebDocumentView高度即实际高度
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight = 0.0f;
if([webView.subviews count] > 0)
{
UIView *scrollerView = webView.subviews[0];
if([scrollerView.subviews count] >
0)
{
UIView *webDocView = scrollerView.subviews.lastObject;
if ([webDocView isKindOfClass:[NSClassFromString(@UIWebDocumentView)class]])
{
webViewHeight = webDocView.frame.size.height;//获取文档的高度
webView.frame=webDocView.frame;
//更新UIWebView的高度
}
}
}
}



你可能感兴趣的:(html,UIWebView,文本图片自适应高度)