首先,将要引用的CSS样式导入到工程文件,然后我们可以自己拼装一个网页并引用这个样式,具体代码实现如下:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *title = @"My Awesome Page";
NSString *cssName =@"screen.css";
NSString *h1=@"Hello there !";
NSString *p1 =@"Thanks for visiting my web page.";
NSString *p2 =@"I hope you like it."
NSMutableString *htmlString =[[NSMutableString alloc]initWithString:@"<html>"];
[htmlString appendString:@"<head>"];
[htmlString appendString:@"<title>"];
[htmlString appendString:title];
[htmlString appendString:@"<link rel =\"stylesheet\" href = \" "];
[htmlString appendString:cssName];
[htmlString appendString:@"\" type=\"text/css\" />"];
[htmlString appendString:@"</head>"];
[htmlString appendString:@"<body>"];
[htmlString appendString:"<h1 class = \"loud\" >"];
[htmlString appendString:h1];
[htmlString appendString:@"</h1>"];
[htmlString appendString:@"<p id=\"lighlight\">"];
[htmlString appendString:p1];
[htmlString appendString:@"</p>"];
[htmlString appendString:@"<p>"];
[htmlString appendString:p2];
[htmlString appendString:@"</p>"];
[htmlString appendString:@"<ul>"];
[htmlString appendString:@"<li class =\"loud\">Pizza</li>"];
[htmlString appendString:@"<li>Beer</li>"];
[htmlString appendString:"<li>Dogs</li>"];
[htmlString appendString:@"</ul>"];
[htmlString appendString:@"</body>"];
[htmlStirng appendString:@"</html>"];
NSString *path =[[NSBundle mainBundle]bundlePath];
NSURL *baseURL =[NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
}
screen.css中的内容
body{
font-size:12px;
font-weight:bold;
font-family: Arial;
}
a { font-style: italic; }
h1 a { font-style:italic; }
.loud {font-style: italic; }
#highlight { background-color: yellow; }