在iOS实际的开发中,使用UIWebView来加载数据使用的场景特别多。很多时候我们会动态的从服务器获取一段HTML的内容,然后App这边动态的处理这段HTML内容用于展示在UIWebView上。使用到的API接口为:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;传统的方法
由于HTML内容通常是变化的,所以我们需要动态生成HTML代码。通常我们从服务器端获取到标题、时间、作者和对应的内容,然后我们需要对这些数据处理之后拼接成一段HTML字符串。对于传统的做法是将上面的需要替换的内容填写一些占位符,放到指定的文件中如(content.html),如下所示:
<!DOCTYPE html><html> <head> <title>key_title</title> </head> <body> <div> <div> <h2 id="key-title">key_title</h2> <div>key_date key_author</div> <hr/> </div> <div>key_content</div> </div> </body></html>
然后在指定的地方使用如下的方式动态生成HTML代码:
- (NSString *)loadHTMLByStringFormat:(NSDictionary *)data{ NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"html"]; NSMutableString *html = [[NSMutableString alloc] initWithContentsOfFile:templatePath encoding:NSUTF8StringEncoding error:nil]; [html replaceOccurrencesOfString:@"key_title" withString:data[@"title"] options:NSCaseInsensitiveSearch range:NSMakeRange(0, html.length)]; [html replaceOccurrencesOfString:@"key_author" withString:data[@"author"] options:NSCaseInsensitiveSearch range:NSMakeRange(0, html.length)]; [html replaceOccurrencesOfString:@"key_date" withString:data[@"date"] options:NSCaseInsensitiveSearch range:NSMakeRange(0, html.length)]; [html replaceOccurrencesOfString:@"key_content" withString:data[@"content"] options:NSCaseInsensitiveSearch range:NSMakeRange(0, html.length)]; return html;}
生成界面如下
在实际的使用中发现还是存在不少的问题,比如我们需要对数据进行预先处理的时候需要写大量的
- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
这样的替换,而且对于一些特殊的字符还需要进行特殊处理等,实在不是太友好,这样就需要一个引擎来专门处理这些事情,本文主要介绍MGTemplateEngine和GRMustache的使用。
MGTemplateEngineMGTemplateEngine是AFNetworking的作者Matt的作品,它是一个比较流行的模板引擎,它的模板语言比较类似于Smarty、FreeMarker和Django。另外它可以支持自定义的Filter(以便实现自定义的渲染逻辑),需要依赖正则表达式的工具类RegexKit。
创建模板
<!DOCTYPE html><html> <head> <title>fs_title</title> </head> <body> <div> <div> <h2 id="fs-title">fs_title</h2> <div>fs_date fs_author</div> <hr/> </div> <div>fs_content</div> </div> </body></html>渲染生成HTML字符串
// 第二种MGTemplateEngine MGTemplateEngine *engine = [MGTemplateEngine templateEngine]; [engine setDelegate:self]; [engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]]; NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"MGTemplateEngineTemplate" ofType:@"html"]; // 第一种赋值方式 [engine setObject:variables[@"title"] forKey:@"title"]; [engine setObject:variables[@"author"] forKey:@"author"]; [engine setObject:variables[@"date"] forKey:@"date"]; [engine setObject:variables[@"content"] forKey:@"content"]; NSString *htmlString = [engine processTemplateInFileAtPath:templatePath withVariables:nil]; // 第二种赋值方式 // Process the template and display the results.// NSString *htmlString = [engine processTemplateInFileAtPath:templatePath withVariables:variables]; NSLog(@"Processed template:\r%@", htmlString); [self.webView loadHTMLString:htmlString baseURL:nil];
说明
(1)MGTemplateEngine提供的示例程序是运行在Mac OS上的,如果要使用到iOS上面需要引入Foundation框架,还需要个UIKit (2)对于运行在Xcode6以上的环境下创建的工程由于没有PCH文件可能会报错,需要在MGTemplateEngine的各个头文件中引入Foundation框架 (3)MGTemplateEngine在GitHub上的地址为https://github.com/mattgemmell/MGTemplateEngine。(4) 如果要算出这个webview的高度 在div加上
NSString *heightString = [webViewstringByEvaluatingJavaScriptFromString:@"document.getElementById(\"container\").offsetHeight;"];
GRMustacheGRMustache使用方法 《GRMustache Document》
举个例子
A typical Mustache template:模板Hello fs_nameYou have just won fs_value dollars!fs_#in_caWell, fs_taxed_value dollars, after taxes.fs_/in_caGiven the following hash:赋值
{ "name": "Chris", "value": 10000, "taxed_value": 10000 - (10000 * 0.4), "in_ca": true}Will produce the following:得出的网页
Hello ChrisYou have just won 10000 dollars!Well, 6000.0 dollars, after taxes.
代码:
NSString *template = [[NSBundle mainBundle] pathForResource:@"MGTemplateEngineTemplate" ofType:@"html"];// 注意要对路径进行UTF8编码 不然会有问题NSString *templatePath = [NSString stringWithContentsOfFile:template encoding:NSUTF8StringEncoding error:nil];NSString *htmlString = [GRMustacheTemplate renderObject:variables fromString:templatePath error:nil];[self.webView loadHTMLString:htmlString baseURL:nil];

本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati

本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

本文討論了視口元標籤,這對於移動設備上的響應式Web設計至關重要。它解釋瞭如何正確使用確保最佳的內容縮放和用戶交互,而濫用可能會導致設計和可訪問性問題。

本文解釋了HTML5&lt; time&gt;語義日期/時間表示的元素。 它強調了DateTime屬性對機器可讀性(ISO 8601格式)的重要性,並在人類可讀文本旁邊,增強Accessibilit

本文討論了使用HTML5表單驗證屬性,例如必需的,圖案,最小,最大和長度限制,以直接在瀏覽器中驗證用戶輸入。

本文討論了&lt; iframe&gt;將外部內容嵌入網頁,其常見用途,安全風險以及諸如對象標籤和API等替代方案的目的。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中