• iOS wkwebview和 uiwebview 内容图片自适应大小


    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    //    2、都有效果
        NSString *js=@"var script = document.createElement('script');"
        "script.type = 'text/javascript';"
        "script.text = "function ResizeImages() { "
        "var myimg,oldwidth;"
        "var maxwidth = %f;"
        "for(i=0;i <document.images.length;i++){"
        "myimg = document.images[i];"
        "if(myimg.width > maxwidth){"
        "oldwidth = myimg.width;"
        "myimg.width = %f;"
        "}"
        "}"
        "}";"
        "document.getElementsByTagName('head')[0].appendChild(script);";
        js=[NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
        [webView stringByEvaluatingJavaScriptFromString:js];
        [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    }
    

    wkwebview:

     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;
        _myWeb = [[WKWebView alloc]initWithFrame:CGRectMake(0, kHeight(60), kGetWidth(self.view.bounds), kGetHeigh(self.view.bounds) - kHeight(70)) configuration:wkWebConfig];
    
  • 相关阅读:
    Vim 使用设置
    stm32之CAN发送、接收详解
    stm32内部的CAN总线
    stm32之CAN总线基础
    JavaScript之Ajax
    JavaScript之insertBefore()和自定义insertAfter()的用法。
    JavaScript之向文档中添加元素和内容的方法
    JavaScript之共享onload
    JavaScrtip之JS最佳实践
    XX秘籍
  • 原文地址:https://www.cnblogs.com/niit-soft-518/p/9392005.html
Copyright © 2020-2023  润新知