1. 程式人生 > >iOS webView新增網路css和網路html

iOS webView新增網路css和網路html

建立webVIew

- (UIWebView *)webView
{
    if (!_ webView)
    {
        _ webView = [UIWebView new];
        [self.view addSubview:_webView];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.opaque = NO;//設定透明 解決底部黑條問題
    }
    return _webView;
}

新增html和css

//desc可以是本地的也可以是網路請求的
NSString *desc = @"<p style=\"color: red\">哈哈哈哈哈哈</p><h1>哈哈哈哈哈哈</h1><h2>哈哈哈哈哈哈</h2><h3>哈哈哈哈哈哈</h3><h4>哈哈哈哈哈哈</h4><img src=\"http://img3.imgtn.bdimg.com/it/u=108228188,2741176027&fm=26&gp=0.jpg\">";

//css
NSString * richtxt_css = [NSString stringWithFormat:@"https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-e4c7a3727d.css
"];
   
NSMutableString *html = [NSMutableString string];   
NSString *css = [NSString stringWithFormat:@"<link href=\"%@\" type=\"text/css\" rel=\"stylesheet\">",richtxt_css];
[html appendString:[NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">%@</head> <body><div class=\"appview\">%@</div><body></html>",css,desc]];

[self.webView loadHTMLString:html baseURL:nil];