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" onload='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>"]];
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 alloc] initWithUrl: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 URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 && [(NSString *)[components objectAtIndex:0]
isEqualToString:@"myweb"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])
{
// NSLog(@"%@",[components objectAtIndex:2]);
}
return NO;
}
return YES;
}
self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];