ios UIWebView 展示HTML代码(UITableView 中添加webview 动态计算高度)

上代码


@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, assign) CGFloat footerHeight;

- (UIWebView *)webView   {
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 500)];
        _webView.delegate = self;
        _webView.scrollView.scrollEnabled = NO;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
    }
    //获取接口返回的html 我这个需要拼接 
    // 用于适配图片大小 宽度为屏宽 高度自适应
    NSString * htmlString = [NSString stringWithFormat:@" %@ ",self.dataModel.content];
    [_webView loadHTMLString:htmlString baseURL:nil];
    return _webView;
}

#pragma  mark -- UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.footerHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, _footerHeight);
    [self.tableView beginUpdates];
    [self.tableView.tableFooterView addSubview:webView];
    [self.tableView endUpdates];
}



#pragma  mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.footerHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = nil;
    footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.footerHeight)];
    [footerView addSubview:self.webView];
    return footerView;
}

注释:我这里是将UIWebView添加在了tableView的foot上

你可能感兴趣的:(学习笔记,ios,uiwebview,html)