WKWebView 加载本地 CSS 样式

2022-02-15  本文已影响0人  ADreamClusive
// 从 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];
}
上一篇下一篇

猜你喜欢

热点阅读