在UITextView里实现类似超链接功能

来源:互联网 发布:大数据相关的书 编辑:程序博客网 时间:2024/06/04 19:49

转载地址:http://www.devdiv.com/forum.php?mod=viewthread&tid=132904

用UITextView的dataDetectorTypes属性可完成类似功能

此属性可以设定使电话号码、网址、电子邮件和符合格式的日期等文字变为链接文字。
电话号码点击后拨出电话,网址点击后会用Safari打开,电子邮件会用mail打开,而符合格式的日期会弹出一个ActionSheet,有创建事件,在Calendar中显示,和拷贝三个选项。
01enum {
02    UIDataDetectorTypePhoneNumber   = 1 << 0,          // Phone number detection
03    UIDataDetectorTypeLink          = 1 << 1,          // URL detection   
04#if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
05    UIDataDetectorTypeAddress       = 1 << 2,          // Street address detection
06    UIDataDetectorTypeCalendarEvent = 1 << 3,          // Event detection
07#endif   
08 
09    UIDataDetectorTypeNone          = 0,               // No detection at all
10    UIDataDetectorTypeAll           = NSUIntegerMax    // All types
11};
12typedef NSUInteger UIDataDetectorTypes;

以上是UIKit框架中,UIDataDetectors.h文件内关于UIDataDetectorTypes的定义。由定义可以看出,我们可以使用|的关系来指定自己想要的链接化文字的方式。
测试代码:
01UITextView *mtextview = [[UITextView alloc] initWithFrame:CGRectMake(550, 360, 200, 200)];
02        mtextview.backgroundColor = [UIColor grayColor];
03        mtextview.dataDetectorTypes = UIDataDetectorTypeAll;
04        mtextview.editable = NO;(必须的)
05        mtextview.text = @"My phone number is +8602980000000.\r\n"
06        "My personal web site <a href="\"http://www.xxxxxx.com.\"" target="\"_blank\"">www.xxxxxx.com.</a>\r\n"
07        "My E-mail address is <a href="\"mailto:XXXXX@gmail.com\"">XXXXX@gmail.com</a>.\r\n"
08        "I was born in 1900-01-01.";
09        [self addSubview:mtextview];
10        [mtextview release];

0 0
原创粉丝点击