设置UIWebView透明


[cpp] ​​view plain​​​​copy​​​​print​​​​?​


  1. [webview setBackgroundColor:[UIColor clearColor]];  

  2. webview.opaque = NO;  



禁止UIWebView滚动 


[cpp] ​​view plain​​​​copy​​​​print​​​​?​


  1. webView.scrollView.bounces = NO; //__IPHONE_5_0  

[cpp] ​​view plain​​​​copy​​​​print​​​​?​


  1. UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];  
  2. scrollView.bounces = NO;   



获取UIWebView高度


[cpp] ​​view plain​​​​copy​​​​print​​​​?​


  1. - (void)webViewDidFinishLoad:(UIWebView *)webView1  
  2. {  
  3.     UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];  
  4.     CGFloat webViewHeight = [scrollView contentSize].height;  
  5.     NSString *curHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];  

  6.     CGRect newFrame = webView.frame;  
  7.     newFrame.size.height = webViewHeight;  
  8.     webView.frame = newFrame;   
  9. }  



使用JS给UIWebView添加事件响应

1.首先定义事件的JavaScript


[cpp] ​​view plain​​​​copy​​​​print​​​​?​


  1. // timeStamp 微秒  
  2. static NSString * const webTouchJavaScriptString =  
  3.     @"<script language=\"javascript\">document.ontouchstart=function(event){\  
  4.     x=event.targetTouches[0].clientX;\  
  5.     y=event.targetTouches[0].clientY;\  
  6.     time=event.timeStamp;\  
  7.     document.location=\"wiweb:touch:start:\"+x+\":\"+y+\":\"+time;};\  
  8.     document.ontouchmove=function(event){\  
  9.     x=event.targetTouches[0].clientX;\  
  10.     y=event.targetTouches[0].clientY;\  
  11.     document.location=\"wiweb:touch:move:\"+x+\":\"+y;};\  
  12.     document.ontouchcancel=function(event){\  
  13.     document.location=\"wiweb:touch:cancel\";};\  
  14.     document.ontouchend=function(event){\  
  15.     time=event.timeStamp;\  
  16.     document.location=\"wiweb:touch:end:\"+time;}; </script>";  

2.组织字符串



[cpp] ​​view plain​​​​copy​​​​print​​​​?​


  1. NSString *webviewText = @"<style>body{margin:0;background-color:transparent;color:#000000;word-wrap:break-word;word-break:break-all;font:18px/22px system}</style>";  
  2. NSString *htmlString = [webviewText stringByAppendingFormat:@"%@", @"自定SDFSDFSDFSDF义字体fsdgjdlagj asdkgjksdh卡号给卡仕达;逛了会街啊啊流口水 http://www.baidu.com 的感觉卡拉;四大金刚;拉开始打工绿卡;但是结果来看;就爱上的看过就卡的;上来讲赶快来;啊都是经过后ihgoiadsg;肯定是噶上的好;拉克丝的价格爱国阿斯顿改了可"];  

  3. NSString *newHTMLString=[htmlString stringByAppendingString:webTouchJavaScriptString];  

3.事件响应



[cpp] ​​view plain​​​​copy​​​​print​​​​?​


  1. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType  
  2. {  
  3.     BOOL result = YES;  
  4.     NSURL *requestURL =[[request URL] retain];  
  5.     NSString *requestString = [[request URL] absoluteString];  
  6.     static BOOL bstart = NO;  
  7.     static BOOL bmove = NO;  
  8.     static double lasttime = 0;  

  9.     NSString *str = [requestURL scheme];  
  10.     if ( ([str isEqualToString:@"http"] || [str isEqualToString:@"https"] || [str isEqualToString:@"mailto"] || [str isEqualToString:@"tel"])  
  11.         && (navigationType == UIWebViewNavigationTypeLinkClicked) )  
  12.     {  
  13.         result = ![[UIApplication sharedApplication] openURL:[requestURL autorelease]];  
  14.     }  
  15.     else  
  16.     {  
  17.         [requestURL release];  
  18.         NSArray *components = [requestString componentsSeparatedByString:@":"];  
  19.         if ([components count] > 2   
  20.             && [(NSString *)[components objectAtIndex:0] isEqualToString:@"wiweb"]  
  21.             && [(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])  
  22.         {  
  23.             NSString *eventString=[components objectAtIndex:2];  
  24.             if ([eventString isEqualToString:@"start"])  
  25.             {  
  26.                 float pointX=[[components objectAtIndex:3] floatValue];  
  27.                 float pointY=[[components objectAtIndex:4] floatValue];  
  28.                 double time=[[components objectAtIndex:5] doubleValue];  
  29.                 CGPoint aPoint = CGPointMake(pointX, pointY);  
  30.                 NSLog(@"start: %@", NSStringFromCGPoint(aPoint));  
  31.                 NSLog(@"start time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);  
  32.                 lasttime = time;  
  33.                 bstart = YES;  
  34.                 bmove = NO;  
  35.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  36.             }  
  37.             else if ([eventString isEqualToString:@"move"])  
  38.             {  
  39.                 float pointX=[[components objectAtIndex:3] floatValue];  
  40.                 float pointY=[[components objectAtIndex:4] floatValue];  
  41.                 CGPoint aPoint=CGPointMake(pointX, pointY);  
  42.                 NSLog(@"move: %@", NSStringFromCGPoint(aPoint));  
  43.                 bmove = YES;  
  44.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  45.             }  
  46.             else if ([eventString isEqualToString:@"cancel"])  
  47.             {  
  48.                 NSLog(@"cancel");  
  49.                 bstart = NO;  
  50.                 bmove = NO;  
  51.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  52.             }  
  53.             else if ([eventString isEqualToString:@"end"])  
  54.             {  
  55.                 double time=[[components objectAtIndex:3] doubleValue];  
  56.                 NSLog(@"end");  
  57.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  58.                 NSLog(@"end time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);  
  59.                 if (bstart && !bmove)  
  60.                 {  
  61.                     if (time - lasttime > 400)  
  62.                     {  
  63.                         NSLog(@"LongPress!!!!!!");  
  64.                     }  
  65.                     else  
  66.                     {  
  67.                         NSLog(@"Click!!!!!!");  
  68.                     }  
  69.                 }  
  70.                 bstart = NO;  
  71.                 bmove = NO;  
  72.             }  

  73.             return NO;  
  74.         }  
  75.     }  

  76.     NSURL *url = [request URL];  
  77.     NSString *curUrl= [url absoluteString];  
  78.     NSLog(@"cururl: %@", curUrl);  

  79.     return result;  
  80. }