Tuesday, 11 August 2015

UITextView should detect links, but otherwise, should propagate touch to view below

.h

@interface LinkOnlyTextView : UITextView
@end

.m

@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 ([self.textStorage attribute:NSLinkAttributeName atIndex:characterIndex effectiveRange:nullptr]) {
        return self;
    }
    return nil;
}

@end

No comments:

Post a Comment