iOS学习--webView笔记

//加载方式,以及用过的东东

- (void)loadRequest:(NSURLRequest *)request;
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;

- (void)goBack;
- (void)goForward;

@property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack;
@property (nonatomic, readonly, getter=canGoForward) BOOL canGoForward;
// 网页加载出错
// NSURLErrorDomain Code=-999 "(null)" UserInfo={NSErrorFailingURLKey=http://www.yxdown.com/news/201509/228280.html, NSErrorFailingURLStringKey=http://www.yxdown.com/news/201509/228280.html}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    //    NSLog(@"%@",error);
    //    [self showAlert:@"加载失败!"];
    //    [self back];
    //

    if([error code] == NSURLErrorCancelled) 
    {
        return;
    }

}
//url为webView的网页链接
[WebV loadRequest:[NSURLRequest requestWithURL:url]];


//获得url的htmlStr,可做网页缓存
-(void)webViewDidFinishLoad:(UIWebView *)webView{

NSString* htmlStr = [WebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];

}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
     NSString * url = request.URL.absoluteString ;
    //获得页面内的url
   //视频,图片
   //子页面(需点击)

    return YES;
}

    //webView自适应
    //contentStr是解析json得出的webView字符串

    ////////////////////////////////////
    [contentStr appendString:@"<html>"];

    [contentStr appendString:@"<head>"];

    [contentStr appendString:@"<meta charset=\"utf-8\">"];

    [contentStr appendString:@"<meta id=\"viewport\" name=\"viewport\" content=\"width=self.view.frame.size.width,initial-scale=1.0,maximum-scale=5.0,user-scalable=yes\" />"];//缩放比例

    [contentStr appendString:@"<meta name=\"apple-mobile-web-app-capable\" content=\"yes\" />"];

    [contentStr appendString:@"<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\" />"];

    [contentStr appendString:@"<meta name=\"black\" name=\"apple-mobile-web-app-status-bar-style\" />"];

    [contentStr appendString:@"<style>iframe{width:100%;}</style>"];  //视频宽度

    [contentStr appendString:@"<style>img{width:100%;}</style>"];  //图片宽度

    [contentStr appendString:@"<style>table{width:100%;}</style>"];  //文字宽度

    [contentStr appendString:@"<title>webview</title>"];

    /////////////////////////////////////


    //获取webView高度,和下方代理方法配合
    NSString * htmlcontent = [NSString stringWithFormat:@"<div id=\"webview_content_wrapper\">%@</div>", contentStr];

    //加载
    [WebV loadHTMLString:htmlcontent baseURL:nil];



-(void)webViewDidFinishLoad:(UIWebView *)webView
{

    //设置字体大小
    NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f",20.];
    [WebV stringByEvaluatingJavaScriptFromString:jsString];

    //获取页面高度(像素)
    CGFloat webH = [[WebV stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    WebV.frame = CGRectMake(0, 91, __zWidth, webH);

    //获取WebView最佳尺寸(点)
    CGSize frame = [WebV sizeThatFits:WebV.frame.size];

    //获取内容实际高度(像素)
    NSString * height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top'))  + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"];

    float height = [height_str floatValue];

    //内容实际高度(像素)* 点和像素的比
    height = height * frame.height / webH;

    //再次设置WebView高度(点)
    WebV.frame = CGRectMake(0, 91, __zWidth, height);

    //滚动视图的滚动区域
    contentV.contentSize = CGSizeMake(0, 91+ height);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值