zoukankan      html  css  js  c++  java
  • UIWebView获得内容的高 高度自适应 宽度自适应

    UIWebView获得内容的高-作出自适应高的UIWebView
    - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]; int height = [height_str intValue]; webView.frame = CGRectMake(0,0,320,height); NSLog(@"height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]); }
    IOS UIWebView截获html并修改便签内容,宽度自适应
    iosuiwebview宽度自适应 
    需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果; 
    方法:通过js截获UIWebView中的html,然后修改html标签内容; 
    实例代码: 
    服务器端html
    Java代码  收藏代码
    <html><head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">   
    <title>网曝四川省一考场时钟慢半小时 老师称这就是命</title></head<body>网曝四川省一考场时钟慢半小时 老师称这就是命</body></html>  
    
    这样显示的结果网页的最小宽度会是device-width;但有时候不需要这个宽度,就需要修改width=device-width为width=myWidth; 
    客户端代码
    Java代码  收藏代码
    - (void)webViewDidFinishLoad:(UIWebView *)webView  
    {     
        //修改服务器页面的meta的值  
        NSString *meta = [NSString stringWithFormat:@"document.getElementsByName("viewport")[0].content = "width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"", webView.frame.size.width];  
        [webView stringByEvaluatingJavaScriptFromString:meta];  
    }  
    
    这样问题就可以解决了 
    
    新增代码: 
    
    Java代码  收藏代码
    //给网页增加utf-8编码  
     [webView stringByEvaluatingJavaScriptFromString:  
     @"var tagHead =document.documentElement.firstChild;"  
      "var tagMeta = document.createElement("meta");"   
      "tagMeta.setAttribute("http-equiv", "Content-Type");"   
      "tagMeta.setAttribute("content", "text/html; charset=utf-8");"   
      "var tagHeadAdd = tagHead.appendChild(tagMeta);"];  
     
    Java代码  收藏代码
    //给网页增加css样式  
        [webView stringByEvaluatingJavaScriptFromString:  
         @"var tagHead =document.documentElement.firstChild;"  
         "var tagStyle = document.createElement("style");"   
         "tagStyle.setAttribute("type", "text/css");"   
         "tagStyle.appendChild(document.createTextNode("BODY{padding: 20pt 15pt}"));"  
         "var tagHeadAdd = tagHead.appendChild(tagStyle);"];  
    
    
    Java代码  收藏代码
    //拦截网页图片  并修改图片大小        
    [webView stringByEvaluatingJavaScriptFromString:  
     @"var script = document.createElement('script');"   
     "script.type = 'text/javascript';"   
     "script.text = "function ResizeImages() { "   
         "var myimg,oldwidth;"  
         "var maxwidth=380;" //缩放系数   
         "for(i=0;i <document.images.length;i++){"   
             "myimg = document.images[i];"  
             "if(myimg.width > maxwidth){"   
                 "oldwidth = myimg.width;"   
                 "myimg.width = maxwidth;"   
                 "myimg.height = myimg.height * (maxwidth/oldwidth);"   
             "}"   
         "}"   
     "}";"   
     "document.getElementsByTagName('head')[0].appendChild(script);"];   
      
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];  
  • 相关阅读:
    【Spring】9、Spring中的事件Event
    【Spring】8、Spring框架中的单例Beans是线程安全的么
    【Spring】7、拦截器HandlerInterceptor
    【Spring】5、利用自定义注解在SpringMVC中实现自定义权限检查
    【Spring】4、Spring中 @Autowired标签与 @Resource标签 的区别
    【Dubbo&&Zookeeper】2、 windows平台dubbo-admin管理平台搭建
    【Dubbo&&Zookeeper】4、 Java实现Dubbo服务提供者及消费者注册
    【Dubbo&&Zookeeper】3、Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法
    【Dubbo&&Zookeeper】1、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    【Spring】3、BeanFactory 和 ApplicationContext的区别
  • 原文地址:https://www.cnblogs.com/someonelikeyou/p/3578500.html
Copyright © 2011-2022 走看看