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

Can*_*ğlu 4 touch uiview ios

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

我需要文本无法选择,所以我遵循/sf/answers/1908549961/并实施:

-(BOOL)canBecomeFirstResponder{
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

它也没有在此之前发送下面的触摸事件,但我已经包括它只是它干扰解决方案的情况.

Dal*_*him 9

您不必阻止文本视图成为第一响应者,而是需要对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
Run Code Online (Sandbox Code Playgroud)

  • 这适用于大多数情况,但需要稍加修改才能适用于所有情况。characterIndexForGlyphAtIndex 返回距离该点最近的字符,因此即使您没有触摸链接,这仍然可以检测到链接已被触摸。获得 glyphIndex 后,您需要获取 glyphRect 并检测该点是否在其中。CGRect glyphRect = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1) inTextContainer:self.textContainer]; 如果 (CGRectContainsPoint(glyphRect, point))... (2认同)

blw*_*ers 5

这是@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
    }
}
Run Code Online (Sandbox Code Playgroud)