Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone SDK-如何将CSS添加到UIWebView中_Iphone_Html_Css_Sdk_Uiwebview - Fatal编程技术网

iPhone SDK-如何将CSS添加到UIWebView中

iPhone SDK-如何将CSS添加到UIWebView中,iphone,html,css,sdk,uiwebview,Iphone,Html,Css,Sdk,Uiwebview,我已经阅读了许多关于我问题的现有问题和答案,但似乎没有一个能具体而简单地回答 我正在我的应用程序中显示许多HTML文件,并希望使用CSS来帮助格式化它们。CSS文件将与HTML文件一起保存在本地 我想我想添加CSS-ref-inline-假设这是正确的方法 我的代码是 - (void)viewDidAppear:(BOOL)animated { [super viewWillAppear:animated]; [adviceContent loadRequest:[NSURLRequest re

我已经阅读了许多关于我问题的现有问题和答案,但似乎没有一个能具体而简单地回答

我正在我的应用程序中显示许多HTML文件,并希望使用CSS来帮助格式化它们。CSS文件将与HTML文件一起保存在本地

我想我想添加CSS-ref-inline-假设这是正确的方法

我的代码是

- (void)viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[adviceContent loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                                                        pathForResource:[advice objectForKey:@"HTML"] ofType:@"html"]
                                                                           isDirectory:NO]]];
[super viewDidAppear:animated];
}

我在HTML文件中插入了我的CSS引用,因此在head标记中< link rel=“stylesheet”type=“text/css”href=“photovaluesCSS\u 2column.css”media=“screen”/

有人能解释一下我错在哪里吗?
谢谢

加载HTML时,您需要为css文件指定一个基本URL,以便您的控制器“知道”css文件的位置

下面是加载使用css文件的html字符串的代码。您可以从文件中加载整个css,或者根据需要修改它

  // HTML files are stored in the main bundle
  NSBundle *bundle = [NSBundle mainBundle]; 
  NSString *path = [bundle bundlePath];

  NSString *filename = @"none";
  NSString *fullPath = [NSBundle pathForResource:filename ofType:@"html" inDirectory:path];


  // load a HTML from a file
  NSString *chapter_filename = [NSString stringWithFormat:@"Section%d", _chapter];      
        NSString *sectionHTMLPath = [[NSBundle mainBundle] pathForResource:chapter_filename ofType:@"html"];

  NSString* htmlContent = [NSString stringWithContentsOfFile:sectionHTMLPath encoding:NSUTF8StringEncoding error:nil];

  // add a generic template and the css file directive
  NSString* htmlString = @"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"he\" lang=\"he\"><head><style type=\"text/css\" media=\"all\">@import \"styles.css\";</style><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>%@</body></html>";


  // load the html into a web view 
  NSURL *url = [NSURL fileURLWithPath:fullPath];
  [_webView loadHTMLString:[NSString stringWithFormat:htmlString, htmlContent] baseURL:url];
//HTML文件存储在主捆绑包中
NSBundle*bundle=[NSBundle mainBundle];
NSString*路径=[bundle bundlePath];
NSString*文件名=@“无”;
NSString*fullPath=[NSBundle pathForResource:类型的文件名:@“html”inDirectory:path];
//从文件加载HTML
NSString*章_文件名=[NSString stringWithFormat:@“第%d节,_章];
NSString*sectionHTMLPath=[[NSBundle mainBundle]路径用于资源:章节\类型的文件名:@“html”];
NSString*htmlContent=[NSString STRINGWITH CONTENTS OFFILE:sectionHTMLPath编码:NSUTF8STRING编码错误:nil];
//添加通用模板和css文件指令
NSString*htmlString=@“@import\”style.css\”;%@”;
//将html加载到web视图中
NSURL*url=[NSURL fileURLWithPath:fullPath];
[\u webView加载htmlString:[NSString stringWithFormat:htmlString,htmlContent]baseURL:url];

谢谢您的回答。我今天要试试这个。从您的回复中,我假设在使用您的解决方案时,我不需要在HTML中引用CSS(内联),因为您将其作为“htmlstring”加载到外部?lWorks非常有用!(别忘了滚动查看键
@import\“style.css\”;
部分!)