简体   繁体   English

在UILabel的一部分上点击手势

[英]Tap Gesture on part of UILabel

I could successfully add tap gestures to a part of UITextView with the following code: 我可以使用以下代码将敲击手势成功添加到UITextView的一部分:

UITextPosition *pos = textView.endOfDocument;// textView ~ UITextView

for (int i=0;i<words*2-1;i++){// *2 since UITextGranularityWord considers a whitespace to be a word

    UITextPosition *pos2 = [textView.tokenizer positionFromPosition:pos toBoundary:UITextGranularityWord inDirection:UITextLayoutDirectionLeft];
    UITextRange *range = [textView textRangeFromPosition:pos toPosition:pos2];
    CGRect resultFrame = [textView firstRectForRange:(UITextRange *)range ];

    UIView* tapViewOnText = [[UIView alloc] initWithFrame:resultFrame];
    [tapViewOnText addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(targetRoutine)]];
    tapViewOnText.tag = 125;
    [textView addSubview:tapViewOnText];

    pos=pos2;
}

I wish to imitate the same behaviour in a UILabel . 我希望在UILabel模仿相同的行为。 The issue is, UITextInputTokenizer (used to tokenize the individual words) is declared in UITextInput.h , and only UITextView & UITextField conform to UITextInput.h ; 问题是, UITextInputTokenizer (使用来标记单个单词)在声明UITextInput.h ,只有UITextViewUITextField符合UITextInput.h ; UILabel does not. UILabel没有。 Is there a workaround for this ?? 有没有解决方法?

Try this. 尝试这个。 Let your label be label : 让您的标签成为label

  //add gesture recognizer to label
  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] init];
  [label addGestureRecognizer:singleTap];
  //setting a text initially to the label
  [label setText:@"hello world i love iphone"];

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];

CGRect rect = label.frame;
CGRect newRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width/2, rect.size.height);

if (CGRectContainsPoint(newRect, touchPoint)) {
    NSLog(@"Hello world");
}
}

Clicking on the first half of label will work (It gives log output). 单击标签的前半部分将起作用(它提供日志输出)。 Not the other half. 没有另一半。

Here is a light-weighted library specially for links in UILabel FRHyperLabel . 这是一个轻量级的库,专门用于UILabel FRHyperLabel中的链接。

To achieve an effect like this: 为了达到这样的效果:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor坐下,一直保持着安静的状态。 Pellentesque quis blandit eros, sit amet vehicula justo. Pellentesque quis blandit eros,坐在amet vehicula justo。 Nam at urna neque. Nam at urna neque。 Maecenas ac sem eu sem porta dictum nec vel tellus. Maecenas ac sem eu sem porta dictum nev veltellus。

use code: 使用代码:

//Step 1: Define a normal attributed string for non-link texts
NSString *string = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis blandit eros, sit amet vehicula justo. Nam at urna neque. Maecenas ac sem eu sem porta dictum nec vel tellus.";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]};

label.attributedText = [[NSAttributedString alloc]initWithString:string attributes:attributes];


//Step 2: Define a selection handler block
void(^handler)(FRHyperLabel *label, NSString *substring) = ^(FRHyperLabel *label, NSString *substring){
    NSLog(@"Selected: %@", substring);
};


//Step 3: Add link substrings
[label setLinksForSubstrings:@[@"Lorem", @"Pellentesque", @"blandit", @"Maecenas"] withLinkHandler:handler];

One option would be to use a non-editable UITextView instead of a UILabel. 一种选择是使用不可编辑的UITextView代替UILabel。 Of course this may or may not be a suitable solution depending on your exact needs. 当然,根据您的确切需求,这可能是不合适的解决方案,也可能不是。

You could try https://github.com/mattt/TTTAttributedLabel and add a link to the label. 您可以尝试https://github.com/mattt/TTTAttributedLabel并添加指向标签的链接。 When the link is pressed you get a action, so part of the label click works only thing you have to would be customizing the link part of the label. 当按下链接时,您将执行操作,因此单击标签的一部分仅对自定义标签的链接部分有效。 I tried this in the past and it worked flawlessly but my client was not interested in using a third party component so duplicated this functionality using UIWebView and HTML. 我过去曾尝试过此方法,但它工作得很好,但是我的客户对使用第三方组件不感兴趣,因此使用UIWebView和HTML复制了此功能。

This is basic code for how can add UITapGestureRecognizer to your control; 这是有关如何将UITapGestureRecognizer添加到控件的基本代码。

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];        
[MyLabelName addGestureRecognizer:singleTap];        
[self.view addSubView:MyLabelName]

This is method that call when you tapped your MyLabelName ; 这是在您点击MyLabelName时调用的方法;

-(void)handleSingleTap:(UILabel *)myLabel
{
    // do your stuff;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM