今天开发 遇到了 UIDataDetectorType,之前没了解过,今天又学习了点。详细看下面详解
还有一个 webView.detectsPhoneNumbers=NO; 去掉webView数字自动解析功能
webView.dataDetectorTypes=UIDataDetectorTypePhoneNumber;//自动检测网页上的电话号码,单击可以拨打
接下来我们来简单讲一下,使用UIDataDetectorTypes自动检测电话、网址和邮箱。我们先来看看UIDataDetectorTypes有哪些枚举值。
UIDataDetectorTypes的枚举值
typedefNS_OPTIONS(NSUInteger,UIDataDetectorTypes){
UIDataDetectorTypePhoneNumber =1<<0, //Phonenumberdetection
UIDataDetectorTypeLink =1<<1, //URLdetection
#if__IPHONE_4_0<=__IPHONE_OS_VERSION_MAX_ALLOWED
UIDataDetectorTypeAddress =1<<2, //Streetaddressdetection
UIDataDetectorTypeCalendarEvent=1<<3, //Eventdetection
#endif
UIDataDetectorTypeNone =0, //Nodetectionatall
UIDataDetectorTypeAll =NSUIntegerMax //Alltypes
};
NS_OPTIONS一般用来定义位移相关操作的枚举值。UIDataDetectorTypeAddress,UIDataDetectorTypeCalendarEvent不在我们考虑的范围了。
UIWebView有dataDetectorTypes属性,UITextView也有dataDetectorTypes属性。我们来UITextView来举例。
创建UITextView
UITextView*textView=[[UITextViewalloc]initWithFrame:self.view.bounds];
textView.font=[UIFontsystemFontOfSize:20];
textView.editable=NO;
textView.text=@"\r\n我的手机号不是:13888888888\r\n\r\n"
"我的博客刚刚在线网址:www.xxxxxx.com\r\n\r\n"
"我的邮箱:worldligang@163.com\r\n\r\n";
[self.viewaddSubview:textView];
运行起来看一下:
可以看出来就是一个简单的文本,电话号码,网址和邮箱都没有检测出来。下面我们来看看dataDetectorTypes作用。
UIDataDetectorTypePhoneNumber检测电话
textView.dataDetectorTypes=UIDataDetectorTypePhoneNumber;
设置textView.dataDetectorTypes的属性,即可检测文本中的电话。效果如下所示:
UIDataDetectorTypeLink检测网址和邮箱
textView.dataDetectorTypes=UIDataDetectorTypeLink;
UIDataDetectorTypeLink检测网址和邮箱的。点击网址会跳转到相应的网页,点击邮箱可以调用系统的发邮件。
UIDataDetectorTypeAll检测电话、网址和邮箱
textView.dataDetectorTypes=UIDataDetectorTypeAll;
UIDataDetectorTypeAll可以检测检测电话、网址和邮箱。效果如下:
专门写了一个iOSStrongDemo以后都在这个iOSStrongDemo里面更新内容。下载地址:iOSStrongDemo