UIWebView是iOS内置的浏览器控件,可以浏览网页、打开文档等 能够加载html/htm、pdf、docx、txt等格式的文件.
一、UIWebView的加载方法
1.通过URL加载
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.superqq.com"]];
[self.view addSubview:webView];
[webView loadRequest:request];
2.加载本地资源
NSString *path = [[NSBundle mainBundle] pathForResource:@"demodemo" ofType:@"html"];
NSURL* url = [NSURL fileURLWithPath:path];//创建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
[webView loadRequest:request];//加载
3.通过NSString加载
NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:path]];
二、代理以及代理方法
//设置代理
webView.delegate = self;
//代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//返回YES,进行加载。通过UIWebViewNavigationType可以得到请求发起的原因
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//开始加载,可以加上风火轮(也叫菊花)
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//完成加载
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//加载出错
}
三、UIWebView的常用的小方法
1.设置自动检测电话
设置这个属性,如果在加载的网页中遇到电话号码,直接单击就可以拨打。
webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;//自动检测网页上的电话号码,单击可以拨打
2.加载等待界面
在加载网页的时候,可能会有一段的等待时间,显示在屏幕上就是空白的,非常不好看,这个时候就可以考虑加一个加载的界面。在webViewDidStartLoad中加入。
- (void) webViewDidStartLoad:(UIWebView *)webView
{
//创建UIActivityIndicatorView背底半透明View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[view setTag:108];
[view setBackgroundColor:[UIColor blackColor]];
[view setAlpha:0.5];
[self.view addSubview:view];
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setCenter:view.center];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[view addSubview:activityIndicator];
[activityIndicator startAnimating];
}
加载完成或失败时,去掉loading效果
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
UIView *view = (UIView*)[self.view viewWithTag:108];
[view removeFromSuperview];
NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[activityIndicator stopAnimating];
UIView *view = (UIView*)[self.view viewWithTag:108];
[view removeFromSuperview];
}
3.UIWebView 的自适应高度
方法一:获取webview中scrovllview的content size进行设置
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight=[webView.scrollView contentSize].height;
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
}
方法二:执行js语句 直接获取html文档的dom高度
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight= [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]floatValue];
// CGFloat webViewHeight= [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"]floatValue];
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
}
方法三:先将UIWebView的高度设为最小,然后再使用sizeThatFits就会返回刚好合适的大小
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGSize actualSize = [webView sizeThatFits:CGSizeZero];
CGRect newFrame = webView.frame;
newFrame.size.height = actualSize.height;
webView.frame = newFrame;
}
方法4.遍历webview子视图 获取UIWebDocumentView高度即实际高度
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight = 0.0f;
if ([webView.subviews count] > 0)
{
UIView *scrollerView = webView.subviews[0];
if ([scrollerView.subviews count] > 0)
{
UIView *webDocView = scrollerView.subviews.lastObject;
if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]])
{
webViewHeight = webDocView.frame.size.height;//获取文档的高度
webView.frame= webDocView.frame; //更新UIWebView 的高度
}
}
}
}
4.设置webview 中字体的大小
在webViewDidFinishLoad做设置
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'"];//修改百分比即可
5.让web view中的图片自适应屏幕的宽度
在webViewDidFinishLoad做设置
NSString *js=@"var script = document.createElement('script');" "script.type = 'text/javascript';" "script.text = \"function ResizeImages() { " "var myimg,oldwidth;" "var maxwidth = %f;" "for(i=0;imaxwidth){"
"oldwidth = myimg.width;"
"myimg.width = %f;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
js=[NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
[webView stringByEvaluatingJavaScriptFromString:js];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];