UITextView应检测链接,否则应传播触摸以查看下面的链接

时间:2015-04-09 12:51:05

标签: ios uiview touch

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

我需要文字无法选择,所以我跟着https://stackoverflow.com/a/27264999/811405并实施了:

-(BOOL)canBecomeFirstResponder{
    return NO;
}

它之前没有发送下面的触摸事件,但是我已经包含它只是它会干扰解决方案。

2 个答案:

答案 0 :(得分:8)

您不需要阻止文本视图成为第一响应者,而是需要继承hitTest方法,以便在链接中发生单击时返回textView,否则返回nil。

@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

答案 1 :(得分:2)

这是@ Dalzhim的答案的Swift版本,结合@jupham的调整来检查point实际上是否包含glyphRect

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

    let glyphIndex = self.layoutManager.glyphIndex(for: point, in: self.textContainer)

    //Ensure the glyphIndex actually matches the point and isn't just the closest glyph to the point
    let glyphRect = self.layoutManager.boundingRect(forGlyphRange: NSRange(location: glyphIndex, length: 1), in: self.textContainer)

    if glyphIndex < self.textStorage.length,
        glyphRect.contains(point),
        self.textStorage.attribute(NSAttributedStringKey.link, at: glyphIndex, effectiveRange: nil) != nil {

        return self
    } else {
        return nil
    }
}