可看
码:
//网页视图
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight)];
//网络地址
NSURL *url = [NSURL URLWithString:@"http://my.csdn.net/my/mycsdn"];
// 网络请求
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
1.// [self.webView loadRequest:request];
//自适应设备的屏幕
self.webView.scalesPageToFit = YES;
[self.view addSubview:self.webView];
2.
//加载HTML
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"html"];
NSString *htmlStr = [[NSString alloc] initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
//拼接字符串
NSString *fullStr = [NSString stringWithFormat:htmlStr, title, content, time, source];
/*
//字符串举例
NSString *s = @"%@%@%@%@%@";
NSString *ss = [NSString stringWithFormat:s,@"1",@"1",@"1",@"1",@"1"];
*/
[self.webView loadHTMLString:fullStr baseURL:nil];
//监控
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:_activityView];
self.navigationItem.rightBarButtonItem = barItem;
self.webView.delegate = self;
#pragma mark - UIWebView Delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"将要开始加载");
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"开始加载");
[_activityView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"加载完成");
[_activityView stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"加载失败,%@",error);
}