【问题标题】:UIWebView dynamic html to templateUIWebView 动态 html 到模板
【发布时间】:2012-03-12 14:45:16
【问题描述】:

您好,我想为从 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.

【问题讨论】:

    标签: iphone html ios uiwebview


    【解决方案1】:

    使用+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];
    

    【讨论】:

    • 这真的很有帮助。感谢您提供了一个很好的例子。
    【解决方案2】:
    //set local path for file
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",
        [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
        @"myTemplate.html"];
    
    //convert htmlContentString to raw data
    NSData *data = [htmlContentString dataUsingEncoding:NSUTF8StringEncoding];
    
    //write data to file
    [data writeToFile:filePath atomically:YES];
    
    //display file in webview
    [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: filePath]]];
    

    【讨论】:

    • 您好,erkanyildiz,感谢您抽出时间回答我的问题。但它如何知道它必须在 html 中使用哪些变量以及必须使用哪些变量?
    • 我的意思是:如何以及在何处使用标题、日、月、desc、url 的 NSString 值???
    • 其实我没有得到你想要的。您收到一些 XML 数据,将其转换为一些 HTML,然后将其显示在 uiwebview 中。那么除了这些,你还想要什么?
    • 嗨 Erkanyldiz,如果我让你感到困惑,对不起。我上面显示的代码工作正常。我希望该代码的 html 移动到外部文件。假设我希望它放在我的服务器上。这样我就可以对 html 进行编辑,而无需每次在 Xcode 中重新构建我的项目。我的 xml 数据设置为 NSString 变量(title、day、month、desc、url)。当然,它们也需要添加。问题是我不知道怎么做。此时(使用上面代码中的内部/内联 html)我通过将 %@ 添加到 html 输出中来获取每个 NSString。希望这能澄清一点。
    • 嗯。我现在明白了。您需要在外部网页上处理 xml 数据。您可以使用客户端(javascript)或网页端(php,asp,...)工具将 xml 格式化为人类可读的 html。
    【解决方案3】:

    模板.html

    <html>
    <head>
    </head>
    <body>
    <div>[[[desc]]]</div>
    <div>[[[title]]]</div>
    <div>[[[day]]]</div>
    </body>
    </html>
    

    代码

    NSString *desc = @"descriptionText";
    NSString *title = @"titleText";
    NSString *day = @"dayText";
    NSString *templateString = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"html"];
    NSMutableString *htmlContentString = [[NSMutableString alloc] initWithString:templateString];
    [htmlContentString stringByReplacingOccurrencesOfString:@"[[[desc]]]" withString:desc];
    [htmlContentString stringByReplacingOccurrencesOfString:@"[[[title]]]" withString:title];
    [htmlContentString stringByReplacingOccurrencesOfString:@"[[[day]]]" withString:day];
    [self.webView loadHTMLString:htmlContentString baseURL:nil];
    [htmlContentString release];
    [self.view addSubview:webView];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 2019-12-10
      • 1970-01-01
      • 2017-04-08
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多