问题描述
当用户单击 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 应用程序相同的结果.当您单击一个名称时,它会推送另一个控制器:
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];
}
编辑
这就是你的 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 中的某个单词来执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!