代码之家  ›  专栏  ›  技术社区  ›  Tommy B.

为生成的HTML UIWebView使用图像和css文件

  •  1
  • Tommy B.  · 技术社区  · 16 年前

    我正在为一个应用程序生成一个HTML文件,在这个HTML文件中有一个指向样式表的链接和一个指向图像的链接。

    以下是我迄今为止尝试的:

    NSMutableString *toReturn = [NSMutableString stringWithCapacity:100];
    NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes.css" ofType:nil];
    
    [toReturn appendFormat:@"<html><head><title></title><link href=\"%@\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" /></head><body>", cssPath];
    

    你知道我怎么做这个吗?

    谢谢

    2 回复  |  直到 16 年前
        1
  •  4
  •   Frank Shearar    13 年前

    this tutorial 不久前。最后我只使用了<样式>但最后还是要加标签。

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    [webView loadHTMLString:htmlString baseURL:baseURL];
    

    -(NSString *)urlForFileNamed:(NSString *) name ofType:(NSString *) type {
      NSString *filePath = [[NSBundle mainBundle] pathForResource: name ofType: type];
      if (!filePath) { NSLog(@"No path found for file %@.%@", name, type); }
      return filePath;
    }
    
    -(void)viewDidLoad {
      [super viewDidLoad];
    
      NSString *path = [[NSBundle mainBundle] bundlePath];
      baseUrl = [NSURL fileURLWithPath: path];
    
      [view loadHTMLString: [self html] baseURL: [NSURL fileURLWithPath: path]];
    }
    
    -(NSString *)html {
      // Obviously the below's just a stub for proper HTML
      return [NSString stringWithFormat: @"<img src=\"@\" />", [self urlForFileNamed: @"foo" ofType: @"png"]];
    }
    
        2
  •  1
  •   zonble    16 年前

    NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes.css" ofType:nil];
    

    NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes" ofType:@"css"];