WebView的应用

原文地址:WebView的应用 作者:林肯公园
webview是一个强大的控件,在它的上面几乎可以实现所有的功能,尤其是html5的到来,把它的引用推向了高潮,下面我就浅谈一下我对其使用的一些小小的心得。
在做应用的过程中很多都需要用网页来排版和使用,比如:看新闻的界面。于是就用到了前端的技术-写html文本。
总结一下:在程序中使用html文本有两种形式:
1、把写好的文件引入到程序中来。
比如有一个你写好的html文本,通过如下代码就可以引入:

 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];    

NSString *path = [documentsDirectory stringByAppendingPathComponent:docName];    

NSURL *url = [NSURL fileURLWithPath:path];

NSURLRequest *request = [NSURLRequest requestWithURL:url];


 

self.myWebView.scalesPageToFit = YES;

[self.myWebView loadRequest:request];

 



或者

 

 

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"238" ofType:@"html"]; 

NSString *file = [[NSString alloc] initWithContentsOfFile:filePath];



字符串file下就可以得到html文本下面所有的html内容。


2、在程序中写html语言。


如果你觉得上面操作后你的网页内容没有写完,那么你可以在file下继续写html语言,如一下代码。


[htmlStr appendString:file];

 

[htmlStr appendString:@"<script language="javascript">document.ontouchstart=function(e){if(e.targetTouches.length == 20){alert('ss')};  }; document.ontouchend=function(){ document.location="myweb:touch:end";  };

document.ontouchmove=function(){if(document.targetTouches.length === 2){ document.location="myweb:touch:move"};}  </script>"];

NSString *string1 = [[NSString alloc] initWithFormat:@"<style> body {margin:0 auto;width:%dpx; padding:0; background: #ffffff}",webViewWidth];

[htmlStr appendString:string1];

[string1 release];

[htmlStr appendString:@".main{padding:8px;word-wrap:break-word; position:relative}"];

[htmlStr appendString:@"p{ padding:0; margin:0}"];

[htmlStr appendString:@".father {width:83px;height:34px;margin:5px 10px 20px 10px;}"];


[htmlStr appendString:@".img {width:30px;height:30px;margin:auto 10px auto 20px;}"];

[htmlStr appendString:@".father p {font-size:13px;color:#14357A;line-height:40px;text-align:center;}"];

[htmlStr appendString:@".comment {width:80px;height:33px;margin:auto auto auto auto}"];

 

[htmlStr appendString:@".father p a {color:#14357A;}"];

[htmlStr appendString:@"a {text-decoration:none;}"];

 

[htmlStr appendString:@".t1{ text-align:center; font-size:19px; color:#134bb0; padding:5px 0}"];

[htmlStr appendString:@".t2{ text-align:center; font-size:14px; color:#666; padding-bottom:2px}"];

[htmlStr appendString:@".t3{ text-align:center; font-size:14px; color:#666; padding-bottom:5px}"];

[htmlStr appendString:@".zhengwen{border:0px solid red;color:#343434;font-size:"];

NSString *string2 = [[NSString alloc]initWithFormat:@"%dpx}",fontSize];

[htmlStr appendString:string2];

[string2 release];

[htmlStr appendString:@".zhengwen p{ text-indent:2em; line-height:1.4em; padding-bottom:0.7em}"];

[htmlStr appendString:@".pic{float:right;padding:0px 9px;position:relative;}"];


[htmlStr appendString:@".small {position:absolute;bottom:-10px;right:0;width:24px;height:25px;}"];

 

[htmlStr appendString:@".small_loading {position:absolute;bottom:-10px;right:0;width:24px;height:25px;}"];

 

[htmlStr appendString:@"</style><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /></head><body><div class="main">"];

 

[htmlStr appendString:[NSString stringWithFormat:@"<p class="t1">%@</p>",(NSString*)[userDataobjectForKey:@"title"]]];

NSDate *today = [NSDate dateWithNaturalLanguageString:(NSString*)[userDataobjectForKey:@"pubDate"]];

today = [today dateByAddingTimeInterval:28800];

//[TEXTLABEL_NEWSTIME setText:[[today description]substringToIndex:20]];

[htmlStr appendString:[NSString stringWithFormat:@"<p class="t2">%@</p>",[[todaydescription]substringToIndex:16]]];

[htmlStr appendString:[NSString stringWithFormat:@"<p class="t3">来源:%@</p>",[userDataobjectForKey:@"source"]]];

 

if (url) 

{

NSString *pic_html = [NSString stringWithFormat:@"<div class="pic">

  <div id="img_loading"><img  src="page_pic_loading.png" />

  <a href="refresh://"><img src="zoom.png" class="small_loading" /></div>

  <a href="Image://"><img id="img" οnlοad='javascript:document.getElementByIdx_x("img_loading").style.display="none";document.getElementByIdx_x("zoom").style.display="block";'

  img_loading").style.display="none";'

  src="%@" /></a>

  <a href="Image://"><img id="zoom" style="display:none" src="zoom.png" class = "small"/></a>

  </div>",url];

[htmlStr appendString:pic_html];

}

[htmlStr appendString:[NSString stringWithFormat:@"<div class="zhengwen">"]];

[htmlStr appendString:[NSString stringWithFormat:@"<P>%@",[(NSString*)[userDataobjectForKey:@"description"]stringByReplacingOccurrencesOfString:@"<br/>" withString:@"</p><p>"]]];

[htmlStr appendString:[NSString stringWithFormat:@"</div></body></html>"]];


通过以上的代码最终写完的html语言的完整字符串就在htmlStr下了,只要让webview去显示它就好了,代码如下:
[_webView loadHTMLString:htmlStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];

通过上面两个任意的方法都可以显示出你所写的html语言样式了。

当然我们可以通过程序使网页调用js:

NSMutableString *stringMore = [[NSMutableString alloc] init];

NSString *js = @"";

if (requestFailed == YES

{

[stringMore appendFormat:@"<div></div>"];

js = [NSStringstringWithFormat:@"document.getElementByIdx_x('clear').innerHTML='%@';",stringMore];

}

else 

{

 

[stringMore appendString:@"<div class="comment"><p><a href="comment://">"];

[stringMore appendFormat:@"%@",userReviewCount];

[stringMore appendString:@"条评论</a></p></div>"];

js = [NSStringstringWithFormat:@"document.getElementByIdx_x('comment').innerHTML='%@';",stringMore];

}

[stringMore release];

[_webView stringByEvaluatingJavaScriptFromString:js];


以上的js代码的意思就使在标签comment下加入stringMore的代码。


webview还有一个应用就是点击一个链接可以调用本地的函数方法:


- (BOOL)webView:(UIWebView *)webView 

shouldStartLoadWithRequest:(NSURLRequest *)request 

 navigationType:(UIWebViewNavigationType)navigationType

{

if ([[[request URL]absoluteString]hasPrefix:@"Image://"]) 

{

NSString *imgUrl = [self getBigImageURL];

 

MyPhotoFadeView *_photoFadeView = [[MyPhotoFadeView allocinitWithUrl:imgUrl];

_photoFadeView.alpha = 0.0;

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:0.5];

_photoFadeView.alpha = 1.0;

[self.view addSubview:_photoFadeView];

self.photoFadeView = _photoFadeView;

[_photoFadeView release];

[UIView commitAnimations];

 

return NO;

}

if ([[[request URL]absoluteString]hasPrefix:@"comment://"]) 

{

[self openReviewInfo];

}

if ([[[request URL]absoluteString]hasPrefix:@"refresh://"]) 

{

[self getReviewCount];

[self setHtmlPageContent];

}

[m_activityIndicatorView stopAnimating];

 

NSString *requestString = [[request URLabsoluteString];

    NSArray *components = [requestString componentsSeparatedByString:@":"];

    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0

  isEqualToString:@"myweb"]) {

        if([(NSString *)[components objectAtIndex:1isEqualToString:@"touch"]) 

        {

           // NSLog(@"%@",[components objectAtIndex:2]);

        }

        return NO;

    }

return YES;

}


以上使用的使webview的一个代理方法:
代码的意思就点击的url链接分别是:image:// comment:// refersh:// 的时候分别响应其对应的方法。

这样webview的应用就差不多了吧!

再分享一下webview的一些小技巧吧!
1、获取html里的title。

self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值