所謂動態Html,指代碼中組合生成的html字符串;
若需要加載本地CSS,圖片,JS文件,使用loadHTMLString baseURL;
有兩種方法:
一,使用絕對路徑,例如:
//獲取文件全路徑
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"M_head@2x" ofType:@"png"];
//代碼加載圖片 需要“file:///”標志;
[_mStrHtmlUrl appendFormat:@"<img src=\"file:///%@\" width=[\"40\" height=\"40\"/> \n",imagePath];
//最后loadHTMLString時,baseURL可為nil;
[webview loadHTMLString:mStringhtml baseURL:nil];
二,使用bundleURL;
//加載文件可以只需要文件名
[_mStringhtml appendFormat:@"<link rel=\"stylesheet\" type=\"text/css\" href=\"Table_normal.css\" />\n "];
//最后loadHTMLString時,使用[[NSBundle mainBundle] bundleURL];
[webview loadHTMLString:weakSelf.mStringhtml baseURL:[[NSBundle mainBundle] bundleURL]];