自動檢測連結地址日期等

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