记录:我这边是由于里面有图片,导致加载计算高度怎么都不对,网上的所有方法都试过了,先看我从后台获取的HTML
-(WKWebView *)webView{
if(!_webView){
_webView=[[WKWebView alloc]initWithFrame:CGRectMake(12, 0, KSCREENWIDTH - 24, KSCREENHEIGHT)];
_webView.navigationDelegate=self;
_webView.backgroundColor=[UIColor whiteColor];
}
return _webView;
}
<p><img src="https://m.youmoapp.com/handWriting/userfiles/1/files/studyshare/nkStudyShare/2023/12/%E7%AF%86%E5%88%BB9%E5%9B%BE-09.jpg"
style="width: 100%;">
</p><p><span style="font-family: 宋体;">字头规范书写及名称</span><span lang="EN-US">61</span><span style="font-family: 宋体;">个</span></p><p class="MsoNormal"><span style="font-family: 宋体;">字底规范书写及名称</span><span lang="EN-US">55</span><span style="font-family: 宋体;">个</span><span lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: 宋体;">字框规范书写及名称</span><span lang="EN-US">6</span><span style="font-family: 宋体;">个</span><span lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US"> </span></p><p class="MsoNormal"><span style="font-family: 宋体;">学习写字,一定要会认会写偏旁部首,把握他们的壁画书写特点,掌握他们的组字规律。</span><span lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US"> </span></p><p class="MsoNormal"><span style="font-family: 宋体;">什么是组字规律?</span><span lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: 宋体;">如上下结构的字,需要把握好从上到下的宽窄关系(以草字头为例)</span><span lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="margin-left: 0cm; text-indent: 0cm;"><span lang="EN-US">1.<span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: "Times New Roman";"> </span></span><span style="font-family: 宋体;">草字头写最宽:苗、茁、节(底部没有横向伸展笔画)</span><span lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="margin-left: 0cm; text-indent: 0cm;"><span lang="EN-US">2.<span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: "Times New Roman";"> </span></span><span style="font-family: 宋体;">伸展笔画写最宽:艾、花、草(底部有横向伸展笔画如:长横、撇捺、竖弯钩、走之等)</span><span lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US"> </span></p><p class="MsoNormal"><span style="font-family: 宋体;">掌握了系统的组字规律,才能准确的写出每个字的字形结构哦!</span><span lang="EN-US"><o:p></o:p></span></p><p></p><p class="MsoNormal"><span lang="EN-US"> </span></p>
我的这个问题,其他的都可以忽略,关键点在于<img
标签问题,
所以处理关键就是替换<img
标签样式
NSString *contentStr = @"你的HTML字符串";
//处理要替换的<img,关键是块显示`display:block`
NSString *result = [NSString stringWithFormat:@"<%@ %@",@"img",@"style='display: block; max-width: 100%;'"];
//替换<img
contentStr = [contentStr stringByReplacingOccurrencesOfString:@"<img" withString:result];
//加入基本的HTML设置
NSString *htmlStr = [NSString stringWithFormat:@"<html><head><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><meta content=\'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;\' name=\'viewport\' /><meta name=\'apple-mobile-web-app-capable\' content=\'yes\'><meta name=\'apple-mobile-web-app-status-bar-style\' content=\'black\'><link rel=\'stylesheet\' type=\'text/css\' /><style type=\'text/css\'> .color{color:#576b95;}</style></head><body><div id=\'content\'>%@</div>", contentStr];
//加载处理好的HTML
self.webView.scrollView.scrollEnabled=NO;
[self.webView loadHTMLString:htmlStr baseURL:nil];
代理中计算重置高度
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
__block CGFloat webViewHeight=0;
//获取内容实际高度(像素)@"document.getElementById(\"content\").offsetHeight;"
[webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error) {
// 此处js字符串采用scrollHeight而不是offsetHeight是因为后者并获取不到高度,看参考资料说是对于加载html字符串的情况下使用后者可以,但如果是和我一样直接加载原站内容使用前者更合适
//获取页面高度,并重置webview的frame
webViewHeight = [result doubleValue];
NSLog(@"%f",webViewHeight);
dispatch_async(dispatch_get_main_queue(), ^{
if (webViewHeight != CGRectGetHeight(webView.frame)) {
//高度重置
CGRect hfr = self.webView.frame;
hfr.size.height = webViewHeight;
self.webView.frame = hfr;
// NSLog(@"%@",@(_webView.scrollView.contentSize.height));
// NSLog(@"%f",_webView.width);
}
});
}];
// NSLog(@"结束加载");
}
网友评论