如何在用户触摸事件的单个UILabel中为某些文本组合调用不同的方法?

时间:2015-04-29 09:19:10

标签: uitableview xamarin xamarin.ios

如下所示,我创建了UITableViewCell,其中包含UILabelUIImage。我发现很难做的是在触摸动作上调用两个单独的方法,用于" John Doe"和#34;海滩"。

点击" John Doe"我希望将用户定向到John Doe的个人资料页面,然后点击" The Beach"我需要把它指向那个"海滩"事件。

如何在单个UILabel上识别两个不同的触摸?

我的UITableViewCell看起来像这样

enter image description here

3 个答案:

答案 0 :(得分:1)

为什么不使用支持添加链接的UILabel替换。

https://github.com/TTTAttributedLabel/TTTAttributedLabel似乎是UILabel的替代品,在许多其他有用的功能中添加了对简单链接检测的支持。

这可以节省你自己写一个。如果你想用你自己的代码做类似的事情,你也可以用它来看看他们是怎么做的。

答案 1 :(得分:0)

im_out

否则您可以使用UIButton,使其透明,即没有图像或名称的自定义类型,并在其上添加UILabel(居中)。然后通过连接IBAction将其用作普通按钮。

答案 2 :(得分:0)

您可以使用ResponsiveLabel。您可以提供正则表达式字符串,以使某些单词可以点击。例如,要突出引号中的单词,您可以这样配置。

PatternTapResponder stringTapAction = ^(NSString *tappedString) {
  NSLog(@"tapped string = %@",tappedString);
};
NSError *error;
NSRegularExpression *expression = [[NSRegularExpression alloc]initWithPattern:@"(\"\\w+\")" options:NSRegularExpressionCaseInsensitive  error:&error];
PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:expression withSearchType:PatternSearchTypeAll withPatternAttributes:@{NSForegroundColorAttributeName:[UIColor brownColor],RLTapResponderAttributeName:stringTapAction}];
[self.responsiveLabel enablePatternDetection:descriptor];