UIWebView是iOS sdk中一个最常用的控件。是内置的浏览器控件,我们可以用它来浏览网页、打开文档等等
用UIWebView在当前程序中打开网页
- NSString *query = [NSString stringWithFormat:@"http://www.baidu.com?q=苹果"];
- NSString *strUrl = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSURL *url = [NSURL URLWithString:strUrl];
- NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
- [webView loadRequest:requestObj];
从APP 包内读取加载一个 HTML 文件
NSString *htmlPath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"webapp/loader.html"];
NSString *htmlString = [NSString stringWithContentsOfFile: htmlPathencoding:NSUTF8StringEncoding error:NULL];
[self.MyWebview loadHTMLString:htmlString baseURL:[NSURLfileURLWithPath:htmlPath]];
让UIWebView背景透明的方法,如下。
web_about.backgroundColor = [UIColor clearColor];
web_about.opaque = NO;
关键是在HTML里:
<body style="background-color: transparent">
text-indent:2em//设置所有段落的首行缩进2格
UIWebView加载Loading效果
- UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
- [view setTag:103];
- [view setBackgroundColor:[UIColor blackColor]];
- [view setAlpha:0.8];
- [self.view addSubview:view];
- UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
- [activityIndicator setCenter:view.center];
- [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
- [view addSubview:activityIndicator];
- [view release];