UIWebView的使用总结
2015-06-19 09:04
232 查看
UIWebView的使用总结
分类:iPhone
Object-c 2012-09-10 17:05
18558人阅读 评论(2)
收藏
举报
componentsfunctionfloaturljavascriptscheme
设置UIWebView透明
[cpp]
view plaincopyprint?
[webview setBackgroundColor:[UIColor clearColor]]; webview.opaque = NO;
[webview setBackgroundColor:[UIColor clearColor]]; webview.opaque = NO;
禁止UIWebView滚动
[cpp]
view plaincopyprint?
webView.scrollView.bounces = NO; //__IPHONE_5_0
webView.scrollView.bounces = NO; //__IPHONE_5_0
[cpp]
view plaincopyprint?
UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0]; scrollView.bounces = NO;
UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0]; scrollView.bounces = NO;
获取UIWebView高度
[cpp]
view plaincopyprint?
- (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; }
- (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
[cpp]
view plaincopyprint?
// 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>";
// 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.组织字符串
[cpp]
view plaincopyprint?
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];
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.事件响应
[cpp]
view plaincopyprint?
- (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;
}
- (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; }
有时间再写个和微博类似的超文本显示的例子
参见点击打开链接
相关文章推荐
- iOS 基础复习
- 源码推荐(6.19):趣味下拉刷新-Replace-iOS,单元格常用的滑动选项-DRCellSlideGestureR
- Android开发adb.exe' and can be executed.错误解决方法
- UIWebView的用法
- xamarin.Android 实现横向滚动导航
- IOS 实现控制器翻页的简单解决方法
- Objective-C 2.0的运行时编程
- java4Android(8)包、打包、包的命名规范(9)包和访问权限(10)继承和访问权限
- Android基础教程之Button事件发送消息到通知栏Notification
- ios7与ios8系统版本不同出现的问题积累
- swift2.0语言新特性
- objective-c中的method swizz实现"猴打补丁"
- objective-c中的method swizz实现"猴打补丁"
- objective-c中的method swizz实现"猴打补丁"
- fiddler移动端抓包
- Android工程中三种库的区别:Private Libraries、Referenced Libraries、Dependency Libraries
- IOS编译报错:objc-class-ref in AppDelegate.o之解决方案
- swift-字符串
- Android 拍照 图库的各种问题.
- Java中Object类hashCode的底层实现