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 我这个需要拼接<html><body> </body></html>
    //<style>img{width:100%% !important;}</style> 用于适配图片大小 宽度为屏宽 高度自适应
    NSString * htmlString = [NSString stringWithFormat:@"<html><body><style>img{width:100%% !important;}</style> %@ </body></html>",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上
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值