您的位置:首页 > 移动开发

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

2017-08-04 11:36 513 查看
上代码

@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上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uiwebview html