UIWebView的使用总结

设置UIWebView透明

[webview setBackgroundColor:[UIColor clearColor]];

webview.opaque = NO;


禁止UIWebView滚动 

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



获取UIWebView高度

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

    CGRect newFrame = webView.frame;
    newFrame.size.height = webViewHeight;
    webView.frame = newFrame; 
}



使用JS给UIWebView添加事件响应

1.首先定义事件的JavaScript

// timeStamp 微秒
static NSString * const webTouchJavaScriptString =
    @"<script language=\"javascript\">document.ontouchstart=function(event){\
    x=event.targetTouches[0].clientX;\
    y=event.targetTouches[0].clientY;\
    time=event.timeStamp;\
    document.location=\"wiweb:touch:start:\"+x+\":\"+y+\":\"+time;};\
    document.ontouchmove=function(event){\
    x=event.targetTouches[0].clientX;\
    y=event.targetTouches[0].clientY;\
    document.location=\"wiweb:touch:move:\"+x+\":\"+y;};\
    document.ontouchcancel=function(event){\
    document.location=\"wiweb:touch:cancel\";};\
    document.ontouchend=function(event){\
    time=event.timeStamp;\
    document.location=\"wiweb:touch:end:\"+time;}; </script>";


2.组织字符串

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

3.事件响应

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    BOOL result = YES;
    NSURL *requestURL =[[request URL] retain];
    NSString *requestString = [[request URL] absoluteString];
    static BOOL bstart = NO;
    static BOOL bmove = NO;
    static double lasttime = 0;
    
    NSString *str = [requestURL scheme];
    if ( ([str isEqualToString:@"http"] || [str isEqualToString:@"https"] || [str isEqualToString:@"mailto"] || [str isEqualToString:@"tel"])
        && (navigationType == UIWebViewNavigationTypeLinkClicked) )
    {
        result = ![[UIApplication sharedApplication] openURL:[requestURL autorelease]];
    }
    else
    {
        [requestURL release];
        NSArray *components = [requestString componentsSeparatedByString:@":"];
        if ([components count] > 2 
            && [(NSString *)[components objectAtIndex:0] isEqualToString:@"wiweb"]
            && [(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])
        {
            NSString *eventString=[components objectAtIndex:2];
            if ([eventString isEqualToString:@"start"])
            {
                float pointX=[[components objectAtIndex:3] floatValue];
                float pointY=[[components objectAtIndex:4] floatValue];
                double time=[[components objectAtIndex:5] doubleValue];
                CGPoint aPoint = CGPointMake(pointX, pointY);
                NSLog(@"start: %@", NSStringFromCGPoint(aPoint));
                NSLog(@"start time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);
                lasttime = time;
                bstart = YES;
                bmove = NO;
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
            }
            else if ([eventString isEqualToString:@"move"])
            {
                float pointX=[[components objectAtIndex:3] floatValue];
                float pointY=[[components objectAtIndex:4] floatValue];
                CGPoint aPoint=CGPointMake(pointX, pointY);
                NSLog(@"move: %@", NSStringFromCGPoint(aPoint));
                bmove = YES;
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
            }
            else if ([eventString isEqualToString:@"cancel"])
            {
                NSLog(@"cancel");
                bstart = NO;
                bmove = NO;
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
            }
            else if ([eventString isEqualToString:@"end"])
            {
                double time=[[components objectAtIndex:3] doubleValue];
                NSLog(@"end");
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
                NSLog(@"end time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);
                if (bstart && !bmove)
                {
                    if (time - lasttime > 400)
                    {
                        NSLog(@"LongPress!!!!!!");
                    }
                    else
                    {
                        NSLog(@"Click!!!!!!");
                    }
                }
                bstart = NO;
                bmove = NO;
            }
            
            return NO;
        }
    }
    
    NSURL *url = [request URL];
    NSString *curUrl= [url absoluteString];
    NSLog(@"cururl: %@", curUrl);
    
    return result;
}


有时间再写个和微博类似的超文本显示的例子

参见点击打开链接




你可能感兴趣的:(JavaScript,function,Scheme,url,float,Components)