1,webview的初始化跟一般控件没多大区别
。h中;
@property(nonatomic,retain)UIWebView *webView;
。m中;self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 70, 320, self.view.frame.size.height-64-70)];
[_webView setUserInteractionEnabled: YES ]; //是否支持交互
[_webView setDelegate:self ]; //委托<UIWebViewDelegate>
[_webView setOpaque:NO ]; //透明
[ self . view addSubview :_webView]; //加载到自己的view
[self.webView setBackgroundColor:[UIColor whiteColor]];
[self.webView release];
2.现面说一些的特殊设置:NSString *content =[[_dic objectForKey:@"content"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//从字典取出有效值进行解码,
NSMutableString *tempContent = [NSMutableString stringWithString:content];//设置可变字符串
[tempContent replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, tempContent.length)];//字符串替换
NSString *str = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-size: %f;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", 35.0,tempContent];//设置html中文字的大小
[self.webView loadHTMLString:str baseURL:nil];//把内容添加到webview
//[self.webView loadHTMLString:tempContent baseURL:nil];
[_webView setScalesPageToFit:YES];//图片自动适应
a。方法一
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[_dic objectForKey:@"url"]]];//字典取出网址并发出请求
[self.webView loadRequest:request];//把请求放到web view中
[_webView setScalesPageToFit:YES];//图片适应webview大小
NSURL *url = [[NSURL alloc] initWithString:[_dic objectForKey:@"url"]];//取出网址
[_webView loadRequest:[NSURLRequest requestWithURL:url]];直接添加到webview;
3.怎么在webview使用完后把它进行释放,我一般习惯在试图消失的时候进行释放
-(void)viewWillDisappear:(BOOL)animated{
[_webView loadHTMLString:@"" baseURL:nil];
[_webView stopLoading];
[_webView setDelegate:nil];
[_webView removeFromSuperview];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.webView release];
}
NSString *content =[dic2 objectForKey:@"message"] ;//字典取出要用的部分
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];//把字符串转换成数字类型data
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];//把data解析放到数组中
for (NSString *html in array) {//遍历数组,构造结构html
if ( [html hasPrefix:@"text:"]) {//找到以“text”开头的
[htmlString appendString: [NSString stringWithFormat:@"<p> %@</p>", [html substringFromIndex:5]]];//添加段落分割符,并让段落空出两个格
[htmlString replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, htmlString.length)];//字符串替换
} else if ([html hasPrefix:@"img:"]) {//以图片开头的
[htmlString appendString: [NSString stringWithFormat:@"<img src='%@' width='150px' height='250' />", [html substringFromIndex:4]]];//改为图片识别格式,并改变图片的长和宽
[htmlString replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, htmlString.length)];//字符串替换
}
NSRange rangeLink = [html rangeOfString:@"<_link>"];//定义三个range变量
NSRange rangeHttp = [html rangeOfString:@"http"];
NSRange range = [html rangeOfString:@"|"];
if (rangeLink.length != 0) {//判断长度是否为零
NSString *httpStr = [html substringWithRange:NSMakeRange(rangeHttp.location, range.location - rangeHttp.location)];//字符串截取
NSString *str = [NSString stringWithFormat:@"<a href=\"%@\" target=\"_blank\">",httpStr];添加字符串
html = [html stringByReplacingCharactersInRange:NSMakeRange(rangeHttp.location, range.location - rangeHttp.location + 1) withString:@""];//截取字符串
html = [html stringByReplacingOccurrencesOfString:@"<_link>" withString:str];字符串替换
html = [html stringByReplacingOccurrencesOfString:@"</_link>" withString:@"</a>"];字符串替换
[htmlString appendString:html];字符串拼接
// NSLog(@"%@",htmlString);
NSString *str = [NSString stringWithFormat:@"<html> \n"//修改字体
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-size: %f;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", 13.0,htmlString];
[self.webView loadHTMLString:str baseURL:nil];//添加内容
}