iOS文本轉html(H5)

有的時候需要把model里面的字段轉成html格式,防止亂碼,下面是核心代碼。

model.Summary是model里面的一個label的屬性

self.webViewSummay初始化一個Webwiew

NSData*data1=[model.Summary dataUsingEncoding:NSUTF8StringEncoding];

[self.webViewSummay loadData:data1MIMEType:@"text/html"textEncodingName:@"UTF-8"baseURL:nil];

NSBundle *bundle = [NSBundle mainBundle];

NSString *resPath = [bundle resourcePath];

NSString *filePath = [resPath stringByAppendingPathComponent:@"hello.html"];

//初始化一個UIWebView實例

myWebView = [[UIWebView alloc] initWithFrame:self.view.frame];

//加載指定的html文件

[myWebView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initFileURLWithPath:filePath]]];

下面就是我們的主角登場了。注意,這個方法得在WebView加載完成后執行。如下所示:

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

NSString *jsCode = [NSString stringWithFormat:@"alert(1);"];

[myWebView stringByEvaluatingJavaScriptFromString:jsCode];

}

運行程序就可以看到一個由js彈出的彈窗了。

但是這僅僅是Objc調用js而已,怎樣才能用js去調Objc的方法呢?如果能實現這一步,那么我們就可以實現提供一些系統層面的api去供js調用,從而去讓js實現更多功能。查看一下UIWebViewDelegate的文檔,會發現有如下接口

webView:shouldStartLoadWithRequest:navigationType:

Sent before a web view begins loading a frame.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

當一個web視圖開始加載一個frame的時候會觸發它。看起來很符合我們的需求。我們可以在頁面里創建一個iframe,使用js代碼去改變它的src來觸發這個調用,從而實現js對Objc的調用。具體的實現方式就不在此贅述,有興趣的可以看一下我寫的這個簡單測試。


webview ?自適應

_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,0)]; ? _webView.delegate=self;_webView.scrollView.bounces=NO; ? _webView.scrollView.showsHorizontalScrollIndicator=NO; _webView.scrollView.scrollEnabled=NO;[_webView sizeToFit];///////////////////////////// //設置內容,這里包裝一層div,用來獲取內容實際高度(像素),htmlcontent是html格式的字符串//////////////NSString * htmlcontent = [NSString stringWithFormat:@"

%@
", ?htmlcontent];[_webView loadHTMLString:htmlcontent baseURL:nil];////////////////////////////////delegate的方法重 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- (void)webViewDidFinishLoad:(UIWebView *)webView{//獲取頁面高度(像素) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NSString * clientheight_str = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"]; ? ? ? ? ? ? ? floatclientheight =[clientheight_str floatValue];//設置到WebView上webView.frame = ? ? ? ? ? ? ? ? ? ?CGRectMake(0,0, self.view.frame.size.width, clientheight);//獲取WebView最佳尺寸(點) ? ? ? ? ? CGSize frame =[webView sizeThatFits:webView.frame.size];//獲取內容實際高度(像素)

NSString * height_str= [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top'))? + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"];floatheight =[height_str floatValue];//內容實際高度(像素)* 點和像素的比

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?height = height * frame.height /clientheight;//再次設置WebView高度(點 ? ) ? ? ? ? ?webView.frame = CGRectMake(0,0, self.view.frame.size.width, height);

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • iOS開發系列--網絡開發 概覽 大部分應用程序都或多或少會牽扯到網絡開發,例如說新浪微博、微信等,這些應用本身可...
    lichengjin閱讀 3,721評論 2 7
  • http://www.cnblogs.com/mddblog/p/5281748.html 一、整體介紹 UIWe...
    F麥子閱讀 1,266評論 0 2
  • IOS之UIWebView的使用 剛接觸IOS開發1年多,現在對于 混合式 移動端開發越來越流行,因為開發成本上、...
    學無止境666閱讀 45,914評論 5 53
  • 原文 在這里總結一些iOS開發中的小技巧,能大大方便我們的開發,持續更新。 1.UITableView的Group...
    無灃閱讀 795評論 0 2
  • 一、WebView WebView就是一個內嵌瀏覽器控件,在iOS中主要有兩種WebView:UIWebView和...
    iOS祎閱讀 1,119評論 0 2