美文网首页
WKWebView 加载本地 CSS 样式

WKWebView 加载本地 CSS 样式

作者: ADreamClusive | 来源:发表于2022-02-15 21:39 被阅读0次
    // 从 Bundle 中加载 css
    - (void)loadFromBundle {
        NSMutableString *html = [NSMutableString string];
        [html appendString:@"<html><head>"];
        [html appendFormat:@"<link rel=\"stylesheet\" href=\"testwai.css\"></head>"];
        [html appendString:@"<body><p>测试文案</p></body></html>"];
        
        NSURL *myUrl = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
        [_webView loadHTMLString:html baseURL:myUrl];
    }
    
    // 从本地文件中加载 css
    - (void)loadFromLocal {
        NSMutableString *html = [NSMutableString string];
        [html appendString:@"<html><head>"];
        [html appendFormat:@"<link rel=\"stylesheet\" href=\"testwai.css\"></head>"];
        [html appendString:@"<body><p>测试文案</p></body></html>"];
        
        NSString *cssPath = [NSString stringWithFormat:@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)];
        NSURL *myUrl = [NSURL fileURLWithPath:cssPath];
        [_webView loadHTMLString:html baseURL:myUrl];
    }
    

    相关文章

      网友评论

          本文标题:WKWebView 加载本地 CSS 样式

          本文链接:https://www.haomeiwen.com/subject/qwnllrtx.html