WKWebView图片文字大小的自适应

#第一种:初始化的时候赋值(界面自适应大小包括图片和文字)

 WKWebViewConfiguration * configuration=[[WKWebViewConfiguration alloc]init];
    WKPreferences * preferences = [[WKPreferences alloc]init];
    preferences.javaScriptCanOpenWindowsAutomatically = true;

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 *ucontroller =[[WKUserContentController alloc]init];

_webview = [[WKWebView alloc] initWithFrame:CGRectMake(5, 8, myDeviceScreenWidth-10, myDeviceScreenHeight-10-NaviHeight) configuration:configuration];
    
    [self.view addSubview:_webview];

 _webview.UIDelegate = self;
    _webview.navigationDelegate = self;

 

#第二种:在代理面修改(文字样式修改)

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{

  //修改字体大小 300%
    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '230%'" completionHandler:nil];
    //修改字体颜色  #9098b8
    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#000000'" completionHandler:nil];

//修改图片的大小(会变形的)

  [ webView evaluateJavaScript:@"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth,oldheight;"
     "var maxwidth = 1000;"
     "var maxheight = 1400;"
     "for(i=0;i      "myimg = document.images[i];"
     "oldwidth = myimg.width;"
     "oldheight = myimg.height"
     "myimg.width =1000;"
//赋值图片的大小
     "myimg.height=2000;"//赋值图片的大小
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);ResizeImages();" completionHandler:nil];

}

你可能感兴趣的:(OC)