2

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 heard about NSAttributedString but I'm not sure how to make this with it.

What I want to have is the same results as the facebook app. When you click on a name it push another controller :

facebook example

If you can give me some hint, tutorial or whatever you want please.

Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
anasaitali
  • 1,504
  • 21
  • 30

3 Answers3

11

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).

Shirkrin
  • 3,993
  • 1
  • 29
  • 35
hris.to
  • 6,235
  • 3
  • 46
  • 55
4

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];
}
dragonfly
  • 411
  • 3
  • 11
-2

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

Chris
  • 4,593
  • 1
  • 33
  • 37
  • I'm using ios 7 and xcode 5. It doesn't matter if it's via IB or programmatically. Your example is for the whole label. If I have a text in a label like : "This is my sample with [word1] and [word2]". I want to send to my selector the word I tapped (word1 or word2) but I don't want to perform any action if the user tap on anything else. – anasaitali Feb 13 '14 at 09:07
  • Hmm I see what you mean. It wasn't clear from your original question, but I think you should look into something like NSAttributedString. Exactly how him not sure Im afraid, but I will look into it as it will be interesting to see the solution for this – Chris Feb 13 '14 at 14:15
  • Please see http://stackoverflow.com/questions/19332283/detecting-taps-on-attributed-text-in-a-uitextview-on-ios-7 – Chris Feb 14 '14 at 08:28