使用UIDataDetectorTypes自动检测电话、网址和邮箱

上一篇文章UIWebView用法详解及代码分享里面提到:

webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;//自动检测网页上的电话号码,单击可以拨打

接下来我们来简单讲一下,使用UIDataDetectorTypes自动检测电话、网址和邮箱。我们先来看看UIDataDetectorTypes有哪些枚举值。

UIDataDetectorTypes的枚举值

typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {
    UIDataDetectorTypePhoneNumber   = 1 << 0,          // Phone number detection
    UIDataDetectorTypeLink          = 1 << 1,          // URL detection    
#if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    UIDataDetectorTypeAddress       = 1 << 2,          // Street address detection
    UIDataDetectorTypeCalendarEvent = 1 << 3,          // Event detection
#endif    

    UIDataDetectorTypeNone          = 0,               // No detection at all
    UIDataDetectorTypeAll           = NSUIntegerMax    // All types
};

NS_OPTIONS一般用来定义位移相关操作的枚举值。UIDataDetectorTypeAddress,UIDataDetectorTypeCalendarEvent不在我们考虑的范围了。

UIWebView有dataDetectorTypes属性,UITextView也有dataDetectorTypes属性。我们来UITextView来举例。

创建UITextView

UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.font = [UIFont systemFontOfSize: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.view addSubview:textView];

运行起来看一下:

UIDataDetectorTypes1

可以看出来就是一个简单的文本,电话号码,网址和邮箱都没有检测出来。下面我们来看看dataDetectorTypes作用。

UIDataDetectorTypePhoneNumber检测电话

textView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;

设置textView.dataDetectorTypes的属性,即可检测文本中的电话。效果如下所示:

UIDataDetectorTypes2

UIDataDetectorTypeLink检测网址和邮箱

textView.dataDetectorTypes = UIDataDetectorTypeLink;

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

UIDataDetectorTypes3

UIDataDetectorTypeAll检测电话、网址和邮箱

textView.dataDetectorTypes = UIDataDetectorTypeAll;

UIDataDetectorTypeAll可以检测检测电话、网址和邮箱。效果如下:

UIDataDetectorTypes4

专门写了一个iOSStrongDemo以后都在这个iOSStrongDemo里面更新内容。下载地址:iOSStrongDemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值