#iOS问题记录#动态Html加载本地CSS和JS文件

所谓动态Html,指代码中组合生成的html字符串;css

若须要加载本地CSS,图片,JS文件,使用loadHTMLString baseURL;html

 

有两种方法:web

一,使用绝对路径,例如:app

  //获取文件全路径orm

    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"M_head@2x" ofType:@"png"];htm

    //代码加载图片 须要“file:///”标志;图片

    [_mStrHtmlUrl appendFormat:@"<img src=\"file:///%@\" width=[\"40\" height=\"40\"/> \n",imagePath];

webview

  //最后loadHTMLString时,baseURL可为nil;字符串

   [webview loadHTMLString:mStringhtml baseURL:nil];file

 

二,使用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]];

相关文章
相关标签/搜索