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;
}


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

参见点击打开链接




  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值