有的時候需要把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);
}