gpt4 book ai didi

objective-c - iOS UIWebView、CSS & 和百分比

转载 作者:行者123 更新时间:2023-11-28 13:40:09 25 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,该应用程序将带有 CSS 样式表的 html 字符串的内容加载到 UIWebView 中,但是当我使用百分比“%”来定义图像的宽度时,我遇到了问题。它就是行不通!

- (void) loadHTMLContent {

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


textFontSize = 120;

NSString *imggt;
if (item.imggt)
imggt = [NSString stringWithFormat:@"<br /><img src='%@' />",item.imggt];
else
imggt = @"";

NSString *cssCode = [NSString stringWithFormat:@" \n"
"body{ font-family: Georgia; color: #333; margin: 0; padding: 0; line-height: 1.2em; } \n"
"h4, h3, h5{ margin: 0 0 5px 0; padding: 0em; text-shadow: 2px 2px 2px rgba(255, 255, 255, 0.8); } \n"
"h3{ font-family: Helvetica; font-size: 1.2em; letter-spacing: -1px; } \n"
"h3, #autor { color: #00365B; } \n"
"h4,h5{ font-weight: normal; font-size: 0.75em; }\n"
"h4{ color: #0069AA; } \n"
"h5{ text-align:justify; } \n"
"#zigzag { height:15px; background-image:url('zigzag.png');background-repeat:repeat-x; }\n"
"#destacados, #cuerpo{ padding: 0 10px; } \n"
"#destacados{ background-image:url('fndNoticia.png'); padding-bottom: 29px; margin-bottom: 0px; } \n"
"#destacados img { width: 97%; text-align:center; margin: 0 auto; border: 8px solid #FFF; } \n"
"#fecha_autor{ font-size: 0.75em; border-bottom: 1px solid #D7D7D7; height: 20px; padding-bottom: 8px; margin: 13px 0; } \n"
"#fecha{ color: #666;} \n"
"#autor{ float: right; } \n"
"#texto{ font-size: 0.80em; line-height: 1.2em; text-align:justify; } \n"];

NSString *htmlCode = [NSString stringWithFormat:@" \n"

"<html> \n"
" <head> \n"
" <title></title> \n"
" <style>%@</style></head> \n" //CSS
" <body> \n"
" <div id='destacados'> \n"
" <br /><h4>%@</h4> \n" //Category
" <h3>%@</h3> \n" //Title
" <h5>%@</h5>%@ \n" //Subtitle & Imggt in HTML
" </div> \n"
" <div id='zigzag'>&nbsp;</div> \n"
" <div id='cuerpo'> \n"
" <div id='fecha_autor'> \n"
" <span id='fecha'>%@</span> \n" //Date
" <span id='author'>%@</span> \n" //Author
" </div> \n"
" <div id='texto'>%@</div> \n"
" </div> \n"
" %@ \n" //Nielsen code
" </body> \n"
"</html> \n",cssCode,item.category,item.title,item.subtitle,imggt,item.pubDate,item.author,[item.text stringByReplacingOccurrencesOfString:@"[br]" withString:@"<br />"],[AppStaticData getNielsenSiteCensusHTMLImageOfArticle:item.url]];

[webView loadHTMLString:htmlCode baseURL:baseURL];

}

在浏览器上测试的 CSS 和 HTML 代码有效:

iOs and Chrome browsers

问题似乎出在这里:

  "#destacados img { width: 97%; text-align:center; margin: 0 auto; border: 8px solid #FFF; } \n"

好像不能用百分比来设置图片的值。为什么?是否有其他方法可以相对于容器大小缩放原始图像?

最佳答案

在构建 [NSString stringWithFormat] 之前,您必须转义 css 代码中的“%”。如果您甚至不需要从简单的格式构建字符串

NSString *cssCode = @"your css {}";

应该可以。

关于objective-c - iOS UIWebView、CSS & 和百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8006789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com