UITextView如何实现只允许链接交互,不允许选择图片

大家都知道,UITextVview可以用以下方法自动检测text中的链接:

let label = UITextView()
label.dataDetectorTypes = .link
label.isEditable = false

如果我们使用的attributedString后,会发现如果添加Attachment后,会触发一个选择(编辑的GR),如果我们只想要链接的gr,不想要选择的gr,可以遍历并禁用它。方法如下:

传送门:http://stackoverflow.com/questions/18962742/uitextview-link-detection-in-ios-7

NSArray *textViewGestureRecognizers = self.captionTextView.gestureRecognizers;
NSMutableArray *mutableArrayOfGestureRecognizers = [[NSMutableArray alloc] init];
for (UIGestureRecognizer *gestureRecognizer in textViewGestureRecognizers) {
    if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
    } else {
        UILongPressGestureRecognizer *longPressGestureRecognizer = (UILongPressGestureRecognizer *)gestureRecognizer;
        if (longPressGestureRecognizer.minimumPressDuration < 0.3) {
            [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
        }
    }
}
self.captionTextView.gestureRecognizers = mutableArrayOfGestureRecognizers;

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *