Created
June 21, 2016 15:30
-
-
Save lukaskollmer/86d54eb85d4ca318f635df2917f2199b to your computer and use it in GitHub Desktop.
UITextView: Allow tapping links from NSAttributedString, but disallow text selection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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