自动检测链接地址日期等

UITextView 内置支持自动检测各种数据。目前能够自动检测的数据包括:

enum {
   UIDataDetectorTypePhoneNumber   = 1 << 0,
   UIDataDetectorTypeLink          = 1 << 1,
   UIDataDetectorTypeAddress       = 1 << 2,
   UIDataDetectorTypeCalendarEvent = 1 << 3,
   UIDataDetectorTypeNone          = 0,
   UIDataDetectorTypeAll           = NSUIntegerMax
};

启用自动检测

// you may add as many as you like by using the `|` operator between options
textView.dataDetectorTypes = (UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber);

如果启用,文本将在 UITextView 上显示为超链接

可点击的数据

要允许单击链接(这将导致根据数据类型执行不同的操作),你必须确保 UITextView 可选但不可编辑并且启用了用户交互

textView.editable = NO;
textView.selectable = YES;
textView.userInteractionEnabled = YES; // YES by default