webView

2780 ワード

  • webView--画像適応スクリーン
  • HTML修正
  • コード修正
  • -(void)viewDidLoad {
        [super viewDidLoad];
    //   webView
        UIWebView *webView=[[UIWebView alloc]initWithFrame:self.view.bounds];
    //  1、 html 
    //  NSString *path=[[NSBundle mainBundle]pathForResource:@"test" ofType:@"html"];
    //  [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
    
    //  2、URL 
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://gb.wallpapersking.com/top/class108/15041/14eec4d8b4bf1fe0.htm"]]];
    
        [self.view addSubview:webView];
        webView.delegate=self;
    }
    
    -(void)webViewDidFinishLoad:(UIWebView *)webView {
        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  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();"];
    }
    

    要求の内部設定
    //  , 
    NSDictionary *dict = [result objectForKey:@"data"];
    HQNewsDetailModel *model = [HQNewsDetailModel mj_objectWithKeyValues:dict];
    
    /**
     * model.details HTMLString
     * " $img[p].style.width = '100%%';
    "---> * 100% */ NSString *htmlString = [NSString stringWithFormat:@"
    " "
    " "
    " "
    " "" "" "window.onload = function(){
    " "var $img = document.getElementsByTagName('img');
    " "for(var p in $img){
    " " $img[p].style.width = '100%%';
    " "$img[p].style.height ='auto'
    " "}
    " "}" "
    %@" "" "",model.details]; // webView HTMLString [self.webView loadHTMLString:htmlString baseURL:nil];