关于webview的常用设置

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.现面说一些的特殊设置:
1,怎么把解码的html放在webview上面:

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];//图片自动适应

2.怎么把URL的网址直接在webview显示:

 a。方法一

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[_dic objectForKey:@"url"]]];//字典取出网址并发出请求

        [self.webView loadRequest:request];//把请求放到web view中

        [_webView setScalesPageToFit:YES];//图片适应webview大小

b。方法二

 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];

}

4.怎么把以text开头的html加到webview上,并把html的网页链接改为网络识别格式(原文链接)

 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%@</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];//添加内容


      }

    }



        












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值