如何在 xcode 中将 CSS 文件添加到 iphone 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8292652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add CSS file to iphone project in xcode?
提问by Bader
I am working on a web based application for iphone, I have created new css
file to the project this css file doesn't affect the html file, but when I have uploaded the css file a host it worked fine and styled the html
我正在为 iphone 开发一个基于 web 的应用程序,我已经css
为项目创建了新文件,这个 css 文件不会影响 html 文件,但是当我上传了 css 文件时,主机工作正常并设置了 html 样式
What is the problem ?
问题是什么 ?
edit 1
编辑 1
This the head element .
这是头部元素。
css.css
is working fine and is linked to the html
correctly
jquery.mobile-1.0.min.css
is NOT !
css.css
工作正常,并html
正确
链接到jquery.mobile-1.0.min.css
不是!
Despite the both files exist in the same border
尽管这两个文件存在于同一边界
Onotha.com
欧诺莎网
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.0.min.css" />
edit 2
编辑 2
This is the Objective-C
code, I have an exception, I think in the last line of code
这是Objective-C
代码,我有一个例外,我想在最后一行代码中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window makeKeyAndVisible];
NSString *path= [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:NO];
// [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
// NSString *path = [[NSBundle mainBundle] bundlePath];
// NSURL *baseURL = [NSURL fileURLWithPath:path];
// [webView loadHTMLString:htmlString baseURL:baseURL];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"css/jquery.mobile-1.0.min.css" ofType:@"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"MyJS" ofType:@"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:@"%@%@%@", cssString, jsString, path];
[webView loadHTMLString:pageContent baseURL:baseURL];
return YES;
}
edit 3
编辑 3
I got this after using code posted by Srikar
我在使用 Srikar 发布的代码后得到了这个
回答by Srikar Appalaraju
You can load CSS from local project directory
您可以从本地项目目录加载 CSS
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
detail info check this site.
详细信息检查这个网站。
More elaborate code here -
更详细的代码在这里 -
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"MyCSS" ofType:@"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"MyJS" ofType:@"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:@"%@%@%@", cssString, jsString, actualPageMarkup];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:@""]];
More info here
更多信息在这里
回答by Sylphos
I don't know if you have solved this, but I think you have to load in "pageContent" the content of the page instead of the path. Something like this:
我不知道你是否解决了这个问题,但我认为你必须在“pageContent”中加载页面的内容而不是路径。像这样的东西:
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding];
NSString *pageContent = [NSString stringWithFormat:@"%@%@%@", cssString, jsString, htmlString];
[webView loadHTMLString:pageContent baseURL:baseURL];