Perform action by clicking on some word in Uitextview or UILabel

给你一囗甜甜゛ 提交于 2019-12-17 16:26:39

问题


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 :

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


回答1:


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




回答2:


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];
}



回答3:


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



来源:https://stackoverflow.com/questions/21749049/perform-action-by-clicking-on-some-word-in-uitextview-or-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!