webview

ios - webView的使用

2016-05-25  本文已影响3532人  fjytqiu

一. 创建:

 UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

二. 加载方式:

  1. 常用的网页加载方式
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.wtmbuy.com"]];  
- (void)loadRequest:(NSURLRequest *)request;  
  1. 加载html格式的内容(html文件中加载和html格式字符串)
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL; 
  1. 从html文件中加载
NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];  
NSString *filePath  = [resourcePath stringByAppendingPathComponent:@"wtmbuy.html"];  
NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];   
[self.webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]]; 
  1. html格式字符串
NSString *htmlStr = @"<img src=\"test2.png\" />ddd";  
[self.webView loadHTMLString: htmlStr baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];  

baseURL指的是HTMLData所引用的其他文件资源的基本路径,网上说如果baseURL:nil图片信息将不会显示出来~,我试了下,为nil时图片居然能显示,具体待研究

加载html格式备注:

1> 改变文字的大小和颜色

NSString * newHtmlStr = [NSString stringWithFormat:@"<html><head><style>body {font-size:16;color:gray; }</style></head><body>%@</body></html>", htmlStr];

2> 使用css进行图片自适应
在web前端,也就是HTML中,如果只设置图片的宽度,那么高度会根据图片原本尺寸进行缩放。

如果后台返回的HTML代码中,不包含<head>标签,则可以直接在HTML字符串前加上一下面的代码(如果包含<head>,则在<head>标签内部添加)。代码含义是,不管用户以前设置的图片尺寸是多大,都缩放到宽度为320px大小。

<head><style>img{width:320px !important;}</style></head>

若需要根据图片原本大小,宽度小于320px的不缩放,大于320px的缩小到320px,那么在HTML字符串前加上一下代码:

<head><style>img{max-width:320px !important;}</style></head>

Demo:

 NSString * newHtmlStr = [NSString stringWithFormat:@"<head><style>img{width:320 !important;height:320px !important;}</style></head><body>%@</body></html>", htmlStr];

3> 如果图片的尺寸过大,通过设置webView.scalesPageToFit = YES;,做自适应,但发现图片显示正常,但是字体太小。
解决法案:(两个配合)

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    
    NSString *str = @"document.getElementsByTagName('body') 
    [0].style.webkitTextSizeAdjust= '300%'";
    [_webView stringByEvaluatingJavaScriptFromString:str];
}

参数300%,是放大参数,可根据具体要求修改,另外,屏幕的宽度大小对字体大小也有影响,最好做个判断来设置放大参数。

当然有时候会发现,htmlStr中即有图片又有文字时的文字大小和htmlStr中只有文字时的文字大小不一样时,可通过下面方法调节,目前发现下面方法修改文字大小只对htmlStr中只有文字时才有效

NSString * newHtmlStr = [NSString stringWithFormat:@"<html><head><style>body {font-size:16;color:gray; }</style></head><body>%@</body></html>",htmlStr];

3.加载方式

- (void)loadData:(NSData *)data MIMEType:(NSString *)  
MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;

三. UIWebView中几个重要的代理方法

1.- (void )webViewDidStartLoad:(UIWebView *)webView 网页开始加载的时候调用
2.- (void )webViewDidFinishLoad:(UIWebView *)webView 网页加载完成的时候调用
3.- (void)webView:(UIWebView )webView didFailLoadWithError:(NSError )error 网页加载错误的时候调用
4.-(BOOL)webView:(UIWebView
)webView shouldStartLoadWithRequest:(NSURLRequest
)request navigationType:(UIWebViewNavigationType)navigationType;

上一篇下一篇

猜你喜欢

热点阅读