css - 在 webview 中添加一个 css 文件

标签 css objective-c uitableview ios7

我有一个 WebView 。因为我想调用一个 css 文件。在编码下方,我在 WebView 中添加了一个 css 文件,但它没有显示任何内容。 我的 css 文件名为:style.css。第一行代码在 style.css 中。

 p{color:#eadfa8},p a{color:#fff; text-decoration:none;}

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"style" ofType:@"css"];
NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];

[webView loadHTMLString:htmlString baseURL:baseURL];  

在那个文件里面:

最佳答案

Webview 在其中加载 HTML 代码,因此如果您将 CSS 应用于 HTML 代码,那么以下代码将帮助您。只需根据需要替换 CSS 代码即可。它非常适合我:-

NSString* title;

title=@"Test CSS";

NSString* webViewBody;

webViewBody = [NSString stringWithFormat:@"<html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>%@</title><style>body{font-family:\"Helvetica\";font-size: 12px;color: #343838; }body a{color: #343838;text-decoration: underline;} h1{ font-size: 15px;font-weight: bold;margin:0;padding: 0;}h2{ font-size: 12px; font-weight: bold;margin:0;padding: 0;}.text{line-height:25px;text-align:justify;}@media screen and (max-width: 1600px) {#glink{width: 12%%;}} @media screen and (max-width: 980px) {#wrapper{ width: 75%%;}#googleMap{width: 100%%;}#glink{width: 22%%;}#details{width: 100%%;}#info{width: 100%%;}#broker{width: 80%%;}#contact{width: 80%%;}}@media screen and (max-width: 780px) {#wrapper{width: 75%%;}#googleMap{width: 100%%;}#glink{width: 18%%;}#details{width: 100%%;}#info{width: 100%%;}#broker{width: 80%%;}#contact{width: 80%%;}}@media screen and (max-width: 320px) {#wrapper{width: 50%%;}#googleMap{width: 80%%;}#name{width:60%%;}#details{width: 75%%;}#info{width: 75%%;}#broker{width: 80%%;}#contact{width: 80%%;}}</style></head><body><h1>MyHTMLBody</h2></body></html>",title];


 NSString *newWebViewBody = [webViewBody stringByReplacingOccurrencesOfString: @"\\u0153" withString:@""];

[webView loadHTMLString:newWebViewBody baseURL:nil];   

关于css - 在 webview 中添加一个 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334926/

相关文章:

objective-c - 应用程序退出事件

iphone - UITableView 在编辑后取消选择选定的单元格

ios - 更改 iOS 11 大标题颜色

ios - 如何根据swift中的内容增加TableViewCell的高度

ipad - 更改表格 View (分组的表格 View 样式)背景颜色?

CSS 溢出麻烦

html - Bootstrap 容器不是静态的

javascript - 尝试在 reactjs 上创建打字效果

HTML CSS 隐形按钮

ios - UIPageviewcontroller 过渡样式滚动连续