美文网首页
iOS UIDataDetectorType 详解

iOS UIDataDetectorType 详解

作者: LeeRich | 来源:发表于2017-12-12 10:16 被阅读34次

今天同事偶然问到文本匹配,查过资料才知道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];

UIDataDetectorTypePhoneNumber检测电话

textView.dataDetectorTypes=UIDataDetectorTypePhoneNumber;

UIDataDetectorTypeLink检测网址和邮箱

textView.dataDetectorTypes=UIDataDetectorTypeLink;
UIDataDetectorTypeLink检测网址和邮箱的。点击网址会跳转到相应的网页,点击邮箱可以调用系统的发邮件。

UIDataDetectorTypeAll检测电话、网址和邮箱

textView.dataDetectorTypes=UIDataDetectorTypeAll;

相关文章

网友评论

      本文标题:iOS UIDataDetectorType 详解

      本文链接:https://www.haomeiwen.com/subject/iuygixtx.html