WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
WKPreferences *preferences = [[WKPreferences alloc]init];
preferences.javaScriptCanOpenWindowsAutomatically = true;
// 图片自适应高度
NSString *jSString = @"var objs = document.getElementsByTagName('img');for(var i=0;i++){var img = objs[i];img.style.maxWidth = '100%';img.style.height='auto';}";
WKUserScript *wkUserScript = [[WKUserScript alloc]initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
// 修改文字大小
NSString*jScript =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkScript = [[WKUserScript alloc]initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *ucontroller = [[WKUserContentController alloc]init];
[ucontroller addUserScript:wkUserScript];
[ucontroller addUserScript:wkScript];
configuration.preferences = preferences;
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 10512) configuration:configuration];
[self.view addSubview:wkWebView];
self.edgesForExtendedLayout = UIRectEdgeNone;
NSString * htmlString = self.memory;
self.view.backgroundColor = [UIColor whiteColor];
wkWebView.scrollView.bounces = NO;
self.tableView.bounces = NO;
wkWebView.navigationDelegate = self;
[wkWebView loadHTMLString:htmlString baseURL:nil];
self.wkWebView = wkWebView;
- (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 != webView.bzHeight) {
//高度重置
webView.bzHeight =webViewHeight*2;
_scrollView.bzHeight = webViewHeight*2;
NSLog(@"%f",_webView.width);
[self.contentLin layoutIfNeeded];
[self.contentLin setNeedsLayout];
}
self.wkWebView.height = webViewHeight;
});
}];
NSLog(@"结束加载");
}
//获取内容高度
[wkWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"contentSize"]) {
CGSize fitSize = [self.wkWebView sizeThatFits:CGSizeZero];
self.wkWebView.frame = CGRectMake(0, 0, fitSize.width, fitSize.height);
[self.tableView beginUpdates];
[self.tableView setTableHeaderView:self.wkWebView];
[self.tableView endUpdates];
}
}