ios 使用WKWebVeiw直接显示HTML代码

1.首先引入头文件:

#import<WebKit/WebKit.h>

2.创建WKWebView:

//获取view的中点X
    CGFloat midX = self.view.frame.origin.x;
    NSLog(@"midX is %f",midX);
    
    //获取view的中点Y
    CGFloat midY =self.view.frame.origin.y;
    //获取view的高
    CGFloat getWidth = CGRectGetWidth(self.view.frame);
    NSLog(@"getWidth is %f",getWidth);
    
    //获取view的宽
    CGFloat getHeight = CGRectGetHeight(self.view.frame);
    NSLog(@"getHeight is %f",getHeight);
   
    CGRect rect1 = CGRectMake(midX,midY,getWidth,getHeight-50);
    _webView = [[WKWebView alloc] initWithFrame:rect1];
3.设置代理:

(1)实现代理:

@interface NewsWebViewController ()<WKNavigationDelegate,WKUIDelegate>
(2)设置代理:

_webView.navigationDelegate = self;
    _webView.UIDelegate = self;
(3)在页面加载完成后的显示:

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    NSLog(@"didFinishNavigation");
    _tipLael.hidden = YES;
    _webView.hidden = NO;
}
4.显示HTML代码:

    NSString *contentStr2 = [self reSizeImageWithHTML:contentStr];
    [_webView loadHTMLString:contentStr2 baseURL:nil];

    // 最后将webView添加到界面
    [self.view addSubview:_webView];
相关文章
相关标签/搜索