Add a tap gesture to the UILabel part

I have an NSAttributedString:

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"]; NSInteger length = str.length; [str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)]; 

NSMutableAttributedString gets a UILabel value as follows:

 label.attributedText = str; 

How do I make a click gesture (or click something) on ​​another view manager for the words "@clickhere" in the line above?

Thanks!

+6
source share
4 answers

I think the best way is to add a UIGestureRecognizer to your UILabel and check the frame you want.

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [_yourLabel addGestureRecognizer:singleTap]; - (void)handleTap:(UITapGestureRecognizer *)tapRecognizer { CGPoint touchPoint = [tapRecognizer locationInView: _yourLabel]; //Modify the validFrame that you would like to enable the touch //or get the frame from _yourLabel using the NSMutableAttributedString, if possible CGRect validFrame = CGRectMake(0, 0, 300, 44); if(YES == CGRectContainsPoint(validFrame, touchPoint) { //Handle here. } } 
+5
source

Just add a gesture to your shortcut

 [label setUserInteractionEnabled:YES]; UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [label addGestureRecognizer:gesture]; 

manage your gesture area in the way below.

 - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { static CGRect touchableRect = CGRectMake(100.0f, 0.0f, 100.0f, 50.0f); // Give your rect as you need. CGPoint touchPoint = [gestureRecognizer locationInView:self.view]; if (CGRectContainsPoint(touchableRect, touchPoint)) { //User has tap on where you want. Do your other stuff here } } 
+1
source
  UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)]; Tap.numberOfTapsRequired = 1; // label Name Is your Label Name [labelName addGestureRecognizer:Tap]; -(void)tapDetected { //your code } 
0
source

I just wanted to add Ramshad's answer on how to deal with a valid frame.

To do this, you may need to use UITextView instead of UILabel, which does not give you access to how it controls the layout of the text. By disabling editing, selection, and scrolling, a UITextView behaves in much the same way as a UILabel, with the exception of some additions that you have to remove.

For convenience, you can add a small category to the UITextView in which you write a method to check whether a point touches any of the characters in the range.

 - (BOOL)point:(CGPoint)point touchesSomeCharacterInRange:(NSRange)range { NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:range actualCharacterRange:NULL]; BOOL touches = NO; for (NSUInteger index = glyphRange.location; index < glyphRange.location + glyphRange.length; index++) { CGRect rectForGlyphInContainer = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(index, 1) inTextContainer:self.textContainer]; CGRect rectForGlyphInTextView = CGRectOffset(rectForGlyphInContainer, self.textContainerInset.left, self.textContainerInset.top); if (CGRectContainsPoint(rectForGlyphInTextView, point)) { touches = YES; break; } } return touches; } 

This will also work for a piece of text containing several words spreading over several lines due to word wrap. He will also work on localized texts as we deal with printed glyphs.

0
source

Source: https://habr.com/ru/post/981196/


All Articles