通过单击Uitextview或UILabel中的某个字来执行操作 [英] Perform action by clicking on some word in Uitextview or UILabel

查看:222
本文介绍了通过单击Uitextview或UILabel中的某个字来执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击Uitextview(或UIlabel)中的某些格式/特定词时,如何执行某些特定操作(如显示模态或推送控制器)?
我听说过NSAttributedString,但我不知道如何使用它。

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.

我想要的是与Facebook的相同的结果app。当您点击某个名称时,它会推送另一个控制器:

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.

推荐答案

向UITextView添加手势识别器:

Add gesture recognizer to your UITextView:

//bind gesture
[_yourTextView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:delegate action:@selector(didReceiveGestureOnText:)]];

然后只需检查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

这是你的didReceiveGestureOnText方法应该是什么样子:

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

这篇关于通过单击Uitextview或UILabel中的某个字来执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆