iphone - UIWebView动态html转模板

标签 iphone html ios uiwebview

您好,我想为从 xml 请求收到的数据创建一个(内部或外部)html 模板。这是我的代码,目前运行良好:

    NSString* desc = [screenData.jsonVars objectForKey:@"descriptionTXT"];
    NSString* title = [screenData.jsonVars objectForKey:@"titleTXT"];
    NSString* day = [screenData.jsonVars objectForKey:@"dayTXT"];
    NSString* month = [screenData.jsonVars objectForKey:@"monthTXT"];
    NSString* url = [screenData.jsonVars objectForKey:@"dataURL"];
    NSString* htmlContentString = [NSString stringWithFormat:
                                   @"<html>"
                                   "<style type=\"text/css\">"
                                   "body { background-color:transparent; font-family:Marker Felt; font-size:44;color:#fff;}"
                                   "</style>"
                                   "<body>"
                                   "<p style=\"text-align:center;font-size:65px;\">%@</p>"
                                   "<div style=\"color:#ff9900;margin:5px;padding:10px;\">%@ &nbsp;%@</div>"
                                   "<div style=\"color:#000;background:#DBDBDB;margin:5px;padding:10px;\">%@</div>"
                                   "<div style=\"color:#000;background:#ff9900;margin:5px;padding:10px;\"><a href=\"%@\">Go to website</a></div>"
                                   "</body></html>", title, day, month, desc, url];
    [BT_debugger showIt:self:[NSString stringWithFormat:@"This is the HTML: %@", htmlContentString]];
    [self.webView loadHTMLString:htmlContentString baseURL:nil];

    [self.view addSubview:webView];

现在我想取出 html 并从外部或内部文件中读取它。例如在 template1.html

如您所见,它是动态的(它是数据),我希望保持这种状态。

任何人对此有建议 提前致谢,

D.

最佳答案

使用 +stringByReplacingOccurencesOfString:NSMutableString 的答案应该使用 -replaceOccurrencesOfString: 代替。此外,NSBundle -pathForResource: 返回一个路径,而不是文件的实际内容。这是一个有效的示例:

模板

<!-- template.html -->
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <style type="text/css">
            body{
                font-family:"Helvetica Neue UltraLight",helvetica,verdana;
                margin-left:0;
                margin-right:0;
            }
            section{
                padding-left:5px;
                padding-right:5px;
                font-size:12px;
            }
            h1{
                font-size: 16px;
                padding-left:5px;
            }
            img{
                display: block;
                 /* 4:3 dimensions */
                width: 220px;
                height:165px;
            }
        </style>
    </head>
    <body>
       <h1>[[[name]]]</h1>
       <img src="[[[image]]]" alt="" />
       <section>
            <p>[[[full_description]]]</p>
       </section>
    </body>
</html>

代码

// load the template
NSString *path = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"html"];
NSString *template = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSMutableString *html = [NSMutableString stringWithString:template];

// make substitutions
[html replaceOccurrencesOfString:@"[[[name]]]" withString:self.building.name options:NSLiteralSearch range:NSMakeRange(0, html.length)];
[html replaceOccurrencesOfString:@"[[[image]]]" withString:self.building.image options:NSLiteralSearch range:NSMakeRange(0, html.length)];
[html replaceOccurrencesOfString:@"[[[full_description]]]" withString:self.building.fulldescription options:NSLiteralSearch range:NSMakeRange(0, html.length)];

// load html string into webView
[self.webview loadHTMLString:html baseURL:nil];

关于iphone - UIWebView动态html转模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669241/

相关文章:

iphone - Core Data 或 Sqlite 与 FMDB 用于基于 RSS 提要的应用程序?

iphone - 无法为版本化模型创建包文件夹?

苹果手机 : Use table custom cell

html - 为什么 img 没有占用父级的全宽?

html - 绝对位置的链接不起作用

ios - 调试父类(super class)或协议(protocol)扩展/实现时调试嵌入式框架未按预期工作

html - 文本转换 : lowercase is not working in email templates

ios - 不同字体的 UILabel

ios - 以编程方式将 .m4a 转换为 .mp3 文件

ios - 又一个 "Mutated while being enumerated"问题