How can I do to perform some specific action (like showing a modal or pushing a controller) when user click on some formated/specific word in Uitextview (or UIlabel) ? I\'ve
It's hackish, but you can try using TTTAttributedLabel and attach a custom URL to the word/phrase within the label:
TTTAttributedLabel *label;
//after setting the label text:
[label addLinkToURL:[NSURL URLWithString:@"http://www.stackoverflow.com"] withRange:[label.text rangeOfString:@"CLICKABLE TEXT HERE"]];
Then in the delegate method, you call your selected action:
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
// for handling the URL but we just call our action
[self userHasClickedTextInLabel];
}
Add gesture recognizer to your UITextView:
//bind gesture
[_yourTextView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:delegate action:@selector(didReceiveGestureOnText:)]];
And then just check which word is clicked in didReceiveGestureOnText with following code:
+(NSString*)getPressedWordWithRecognizer:(UIGestureRecognizer*)recognizer
{
//get view
UITextView *textView = (UITextView *)recognizer.view;
//get location
CGPoint location = [recognizer locationInView:textView];
UITextPosition *tapPosition = [textView closestPositionToPoint:location];
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
//return string
return [textView textInRange:textRange];
}
EDIT
This is how your didReceiveGestureOnText method should look-like:
-(void)didReceiveGestureOnText:(UITapGestureRecognizer*)recognizer
{
//check if this is actual user
NSString* pressedWord = [delegate getPressedWordWithRecognizer:recognizer];
}
However this will led you in checking strings after all which is in really cool(as it's slow).
You can add a gesture recognizer to the label. eg:
[yourLabel setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[yourLabel addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
You didnt specify which version you are using or whether its via IB or programmatically. This sets up the gesture recognizer on your label. The selector is the action you want to carry out eg performSegue etc. Let me know how this goes