iOS WKWebView、UIWebView获取网页内容高度
参考:
项目UI需要,页面部分内容是网页富文本或者加载的是网页,然后展示的UI高度还跟展示的网页内容高度相关
一般可以拿到的有:FitSize offset scrollView.contentSize
一、加载富文本 1.1 WKWebView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero];
[self.view addSubView:self.webView];
NSString *stringContent = @"xxxx";
// 默认设置文本字体
NSString *htmlTxt = [NSString stringWithFormat:@"<html><body style='font-size:26pt;'>%@</body></html>",stringContent ?: @""];
[self.webView loadHTMLString:htmlTxt baseURL:nil];
WS(weakSelf)
// 隔一段时间再取高度,好让WebView渲染完取的高度更准确
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString *jsTxt = @"document.body.offsetHeight;";
[weakSelf.webView evaluateJavaScript:jsTxt completionHandler:^(id val, NSError *error) {
if(val != nil){
CGSize fittingSize = [weakSelf.webView sizeThatFits:CGSizeZero];
CGFloat offsetHeight = [val floatValue];
CGFloat contentHeight = weakSelf.webView.scrollView.contentSize.height;
NSLog(@"%f %f %f",fittingSize.height,offsetHeight,contentHeight);
CGFloat boxHeight = contentHeight + 50;
// 防止过长,让web内容自己滚动
if(boxHeight>(Main_Screen_Height-30)){
boxHeight = Main_Screen_Height-30;
}
// 设置展示的UI相关高度
xxxxx
}
}];
});
1.2 UIWebView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero];
[self.view addSubView:self.webView];
NSString *stringContent = @"xxxx";
// 默认设置文本字体
NSString *htmlTxt = [NSString stringWithFormat:@"<html><body style='font-size:26pt;'>%@</body></html>",stringContent ?: @""];
[self.webView loadHTMLString:htmlTxt baseURL:nil];
WS(weakSelf)
// 隔一段时间再取高度,好让WebView渲染完取的高度更准确
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString *jsTxt = @"document.body.offsetHeight;";
CGFloat offsetHeight = [[weakSelf.webView stringByEvaluatingJavaScriptFromString:jsTxt] floatValue];
CGSize fittingSize = [weakSelf.webView sizeThatFits:CGSizeZero];
CGFloat contentHeight = weakSelf.webView.scrollView.contentSize.height;
NSLog(@"%f %f %f",fittingSize.height,offsetHeight,contentHeight);
});
二、加载网页 和富文本一样,只需要在对应的 UIWebView
的代理方法 - (void)webViewDidFinishLoad:(UIWebView *)webView
或WKWebView的代理方法处理即可 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
This post is licensed under CC BY 4.0 by the author.