参考文档:
WKWebView的使用和各种坑的解决方法(OC+Swift)
WKWebView的使用及js交互
iOS:WKWebView与UIWebView的区别
需求一、修改WKWebView字体大小和颜色
// LSXMatchDetalVCView.m
// Badminton
//
// Created by mac on 2018/6/13.
// Copyright © 2018年 LSX. All rights reserved.
//
#import "LSXMatchDetalVCView.h"
@interface LSXMatchDetalVCView ()<WKNavigationDelegate>
@end
@implementation LSXMatchDetalVCView{
WKWebView *webView;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self =[super initWithFrame:frame]) {
self.backgroundColor=[UIColor orangeColor];
webView=[[WKWebView alloc]init];
[self addSubview:webView];
webView.backgroundColor=[UIColor whiteColor];
webView.opaque = NO;
webView.navigationDelegate=self;
// 去掉webView的滚动条
for (UIView *subView in [webView subviews])
{
if ([subView isKindOfClass:[UIScrollView class]])
{
// 不显示竖直的滚动条
[(UIScrollView *)subView setShowsVerticalScrollIndicator:NO];
}
}
}
return self;
}
-(void)layoutSubviews{
webView.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49-64);
// 添加额外的滚动附近区域的内容
webView.scrollView.contentInset = UIEdgeInsetsMake(SCREEN_WIDTH/2, 0, 0, 0);
}
-(void)setModel:(LSXMatchDetalModel *)model{
_model=model;
NSString *str =_model.content;
//webView文本
[webView loadHTMLString:str baseURL:nil];
}
// WKNavigationDelegate方法 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//修改字体大小 300%
[ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'" completionHandler:nil];
//修改字体颜色 #9098b8
[ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#9098b8'" completionHandler:nil];
}