WKWebView加载HTML文本(图片自适应)

UITextView 加载html文本(图片自适应)

    NSString * htmlsTr = @"哦吧你不会比谁都不休的白色修身

才能完呢冲破恩微博撇那狗骗你我饿够了呢比你耳边n伪军抹去你问邪恶

\"Gravatar\"

热敷已成为8哦鳄鱼从背后有我饿

\"Gravatar\"

\"File\"去年细纹卡马乔全部下次去把握和刺猬比欧巴

"; NSString * urlStr1 = [NSString stringWithFormat:@"%@",htmlsTr];
    [self.webView loadHTMLString:urlStr1 baseURL:nil];
计算高度注意这里是异步的
//页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable any, NSError * _Nullable error) {
       
        NSString *heightStr = [NSString stringWithFormat:@"%@",any];
        NSLog(@"--高度--%@",heightStr);
        
        
    }];
    
}
-(WKWebView *)webView
{
    if (!_webView) {
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebConfig];
        _webView.navigationDelegate = self;
        _webView.scrollView.bounces = NO;
        _webView.scrollView.alwaysBounceVertical = NO;
        _webView.scrollView.scrollEnabled = NO;
        
        //  [_webView loadHTMLString:<#(nonnull NSString *)#> baseURL:<#(nullable NSURL *)#>]
        
        // [_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil]; 监听contentsize 变化
        [self.bottomView addSubview:_webView];
    }
    return _webView;
}

效果图

WKWebView加载HTML文本(图片自适应)_第1张图片
image.png

你可能感兴趣的:(WKWebView加载HTML文本(图片自适应))