Tap Gesture on part of UILabel
Asked Answered
S

5

5

I could successfully add tap gestures to a part of UITextView with the following code:

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. The issue is, UITextInputTokenizer (used to tokenize the individual words) is declared in UITextInput.h, and only UITextView & UITextFieldconform to UITextInput.h; UILabel does not. Is there a workaround for this ??

Summitry answered 24/7, 2013 at 6:48 Comment(3)
hi friend, have you checked the user interaction behaviour of UILabel, because by default userinteraction is NO of UIlabel, you will have to set it YES.Let me know is it working or not.!!!Drongo
Action on an entire UILabel is not an issue, it's 'part' of UILabel.Summitry
Please check #8812409Adze
R
0

You could try https://github.com/mattt/TTTAttributedLabel and add a link to the label. 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.

Rubbico answered 24/7, 2013 at 7:46 Comment(2)
Finally settled on TTTAttributedLabel. It turns out to be as heavy as a UITextView. :(Summitry
TTTAttributedLabel has height issues. Especially when it comes to emoji.Judaic
L
3

Try this. Let your label be 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.

Legislatorial answered 24/7, 2013 at 10:4 Comment(1)
@n00bProgrammer - You can create your own CGRect newRect based on your needs.Legislatorial
C
3

Here is a light-weighted library specially for links in UILabel FRHyperLabel.

To achieve an effect like this:

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.

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];
Ciceronian answered 23/9, 2015 at 17:44 Comment(0)
P
2

One option would be to use a non-editable UITextView instead of a UILabel. Of course this may or may not be a suitable solution depending on your exact needs.

Palmy answered 24/7, 2013 at 7:0 Comment(2)
UITextViews are out of question. I can only use UILabels.Summitry
@n00bProgrammer, you can make the UITextView behave like UILabel.Mirilla
R
0

You could try https://github.com/mattt/TTTAttributedLabel and add a link to the label. 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.

Rubbico answered 24/7, 2013 at 7:46 Comment(2)
Finally settled on TTTAttributedLabel. It turns out to be as heavy as a UITextView. :(Summitry
TTTAttributedLabel has height issues. Especially when it comes to emoji.Judaic
N
-3

This is basic code for how can add UITapGestureRecognizer to your control;

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;

-(void)handleSingleTap:(UILabel *)myLabel
{
    // do your stuff;
}
Nelan answered 24/7, 2013 at 6:51 Comment(1)
I appreciate your answer, but i do not need a gesture on the entire label, just part of it.Summitry

© 2022 - 2024 — McMap. All rights reserved.