【问题标题】:Add local CSS to Domain Online UIWebView将本地 CSS 添加到域在线 UIWebView
【发布时间】:2015-04-07 09:56:02
【问题描述】:

您好,我不想将 css 样式添加到 UIWebView

CSS 代码

<style>body{ background: none; } .sepLine{ display: none; } .wrapper{ width: 320px; }</style>

UIWebView 代码

NSString *fullURL = @"http://parentportal.npsinternational.edu.sg/nps/Dashboard.aspx";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

我该怎么做

【问题讨论】:

    标签: ios css objective-c uiwebview


    【解决方案1】:

    使用 UIWebView 委托方法 - (void)webViewDidFinishLoad:(UIWebView *)webView 等待页面完成加载,然后使用带有 - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script 的 JavaScript 注入 CSS

    【讨论】:

      【解决方案2】:

      我相信您必须加载 HTML 字符串、附加样式,然后将其加载到 Web 视图。 试试这个代码:

      dispatch_async(dispatch_get_main_queue(), ^{
          NSString *fullURL = @"http://parentportal.npsinternational.edu.sg/nps/Dashboard.aspx";
          NSString *style = @"<style> ... </style>";
      
          NSURL *url = [NSURL URLWithString:fullURL];
      
          NSError* error = nil;
      
          //load HTML string
          NSString* html = [NSString stringWithContentsOfURL:url usedEncoding:NSUTF8StringEncoding error:&error];
          if(!error){
              //append style string
              html = [html stringByAppendingString:style];
      
              //load webview
              [webView loadHTMLString:html baseURL:url];
          }
      });
      

      只是例子,你最好用高级网络工具加载字符串,例如AFNetworking

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 2011-09-09
        • 1970-01-01
        • 2011-09-01
        • 2014-12-26
        相关资源
        最近更新 更多