2012-03-12 15 views
3

こんにちは私はXMLリクエストから受け取ったデータの(内部または外部)HTMLテンプレートを作成したいと思います。これは私が持っているコードであり、それは罰金、これまで働いている:UIWebView動的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.

答えて

1
//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]]]; 
+0

erkanyildizこんにちは、私の質問に答えるために時間を話しためのおかげで:ここで働く例です。しかし、htmlでどのようにどの変数を使用しなければならないのか、どのように知っていますか? – Danny

+0

私は、どのようにNSStringの値をtitle、day、month、desc、urlに使うのか? – Danny

+0

実際に私はあなたが望むものを得られません。いくつかのXMLデータを受け取り、HTMLに変換してuiwebviewに表示します。だから、それらに加えて何をしたいですか? – erkanyildiz

1

template.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]; 
6

を使用しての答え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]; 
+0

これは本当に役に立ちます。素晴らしい例をありがとう。 –

関連する問題