Skip to content

Instantly share code, notes, and snippets.

@lukaskollmer
Created June 21, 2016 15:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskollmer/86d54eb85d4ca318f635df2917f2199b to your computer and use it in GitHub Desktop.
Save lukaskollmer/86d54eb85d4ca318f635df2917f2199b to your computer and use it in GitHub Desktop.
UITextView: Allow tapping links from NSAttributedString, but disallow text selection
/**
Note to self:
This UITextView class extension gets calles every time iOS wants to add a new gesture recognizer to the textView.
It checks for the type of the gesture recognizer (Long Press) and then for the associated action.
Only if the action is ``smallDelayRecognizer:``, the gestuere recognizer will stay enabled.
All otheer long press gestures will be disabled.
Purpose:
Allow user interaction with the links in the attributedText, but disallow text selection
*/
@implementation UITextView (NoFirstResponder)
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
@try {
id targetAndAction = ((NSMutableArray *)[gestureRecognizer valueForKey:@"_targets"]).firstObject;
NSArray <NSString *>*actions = @[@"action=loupeGesture:", // link: no, selection: shows circle loupe and blue selectors for a second
@"action=longDelayRecognizer:", // link: no, selection: no
/*@"action=smallDelayRecognizer:", // link: yes (no long press), selection: no*/
@"action=oneFingerForcePan:", // link: no, selection: shows rectangular loupe for a second, no blue selectors
@"action=_handleRevealGesture:"]; // link: no, selection: no
for (NSString *action in actions) {
if ([[targetAndAction description] containsString:action]) {
[gestureRecognizer setEnabled:false];
}
}
}
@catch (NSException *e) {
}
@finally {
[super addGestureRecognizer: gestureRecognizer];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment