UITextView应该检测链接,否则,应该传播触摸以查看下面的视图 [英] UITextView should detect links, but otherwise, should propagate touch to view below

查看:46
本文介绍了UITextView应该检测链接,否则,应该传播触摸以查看下面的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本视图,我想在其中检测链接,但是当接触点上没有链接时,它应该将触摸传播到下面的视图中(当前没有).它将包含在一个表格视图单元格中,如果用户点击一个链接,它应该进行交互(它可以工作),但是当另一个点被点击时,它应该选择表格视图单元格.

I have a text view, where I want to detect links, but when there isn't a link at the touch point, it should propagate the touch to view below (it currently doesn't). It will be contained in a table view cell, and if the user taps a link, it should interact (it works), but when another point is tapped, it should select the table view cell.

我需要使文本无法选择,因此我遵循了 https://stackoverflow.com/a/27264999/811405并实现:

I needed text to be unselectable, so I've followed https://stackoverflow.com/a/27264999/811405 and implemented:

-(BOOL)canBecomeFirstResponder{
    return NO;
}

在此之前也没有发送以下触摸事件,但是我将其包括在内是为了防止干扰解决方案.

It wasn't sending the touch events below before that too, but I've included it just is case it interferes with a solution.

推荐答案

您需要对 hitTest 方法进行子类化,而不是阻止文本视图成为第一响应者,以使当点击发生在链接内,否则返回nil.

Instead of preventing your text view from becoming first responder, you need to subclass the hitTest method so that it returns the textView when the click occured inside a link and return nil otherwise.

@interface LinkOnlyTextView : UITextView
@end

@implementation LinkOnlyTextView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSUInteger glyphIndex = [self.layoutManager glyphIndexForPoint:point inTextContainer:self.textContainer fractionOfDistanceThroughGlyph:nullptr];
    NSUInteger characterIndex = [self.layoutManager characterIndexForGlyphAtIndex:glyphIndex];
    if (characterIndex < self.textStorage.length) {
        if ([self.textStorage attribute:NSLinkAttributeName atIndex:characterIndex effectiveRange:nullptr]) {
            return self;
        }
    }
    return nil;
}

@end

@blwinters提供了此代码段的Swift版本,在此处对此问题的稍后答案中: https://stackoverflow.com/a/47913329/1279096

There is a Swift version of this code snippet provided by @blwinters in a later answer to this question here: https://stackoverflow.com/a/47913329/1279096

这篇关于UITextView应该检测链接,否则,应该传播触摸以查看下面的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆