添加轻击手势到UILabel的一部分

时间:2015-01-19 05:57:36

标签: ios iphone uilabel nsattributedstring uitapgesturerecognizer

我有一个像这样的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设置为UILabel,如下所示:

label.attributedText = str;

如何在另一个视图控制器中为上面的字符串中的“@clickhere”创建一个轻击手势(或可点击的内容)?

谢谢!

5 个答案:

答案 0 :(得分:5)

我认为,最好的方法是将UIGestureRecognizer添加到UILabel并验证您想要的框架。

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.
     }
}

答案 1 :(得分:1)

只需首先在标签上添加一个手势

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

使用以下方法控制您的手势区域

- (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
    }
}

答案 2 :(得分:0)

  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
   }

答案 3 :(得分:0)

我想补充一下Ramshad的答案,关于如何处理有效的框架。

为此,您可能需要考虑使用UITextView而不是UILabel,它不能让您访问它如何管理文本的布局。通过禁用编辑,选择和滚动,UITextView的行为与UILabel大致相同,只是必须删除一些填充。

为方便起见,您可能希望在UITextView中添加一个小类别,在其中编写一个方法来测试一个点是否接触到范围内的任何字符。

- (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;
}

这也适用于包含多个单词的文本片段,这些单词由于自动换行而分布在多行中。当我们处理打印的字形时,它也适用于本地化文本。

答案 4 :(得分:0)

该想法与已接受的答案相同。这就是Swift中的方法。

  • 假设您已经设置好标签。

    youLabel.text = "please tab me!"

  • 将tapGestuerRecognizer添加到您的标签

    let tap = UITapGestureRecognizer(target: self, action:    #selector(tapAction))
    yourLabel.addGestureRecognizer(tap)
    
  • String扩展方法添加到String中以计算字符串rect。

    extension String {
        func rect(font: UIFont) -> CGRect {
            let label = UILabel(frame: .zero)
            label.font = font
            label.text = self
            label.sizeToFit()
            return label.frame
        }
    }
    
  • 计算点击手势的可用点击矩形。

    let pleaseStringRect = "please".rect(font: yourFont)
    let tapStringRect = "tap".rect(font: yourFont)
    let tapAreaRect = CGRect(x: pleaseStringRect.width, y:   tapStringRect.origin.y, width: tapStringRect.width, height: tapStringRect.height"
    
  • 在操作中做您想要的

     @objc private func tapAction(tap: UITapGestureRecognizer) {
       let position = tap.location(in: content)
       guard reporterFirstNameRect.contains(position) else {
           return
       }
       // Do the tap action stuff
    }
    

    就是这样,很高兴编码。