Skip to content

Instantly share code, notes, and snippets.

@fmtonakai
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmtonakai/11144531 to your computer and use it in GitHub Desktop.
Save fmtonakai/11144531 to your computer and use it in GitHub Desktop.
TextViewのリンクのところだけ反応させてあとはスルーする
// implementation of UITextView subclass
// editableプロパティをNO, selectableプロパティをYESにする
// リンクのところ以外のタッチを透過する
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint p = point;
p.y -= self.textContainerInset.top;
p.x -= self.textContainerInset.left;
NSInteger i = [self.layoutManager characterIndexForPoint:p inTextContainer:self.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];
NSRange effectiveRange;
NSDictionary *attr = [self.textStorage attributesAtIndex:i effectiveRange:&effectiveRange];
if (attr[NSLinkAttributeName]) {
__block BOOL touchingLink = NO;
NSInteger glyphIndex = [self.layoutManager glyphIndexForCharacterAtIndex:i];
[self.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(glyphIndex, 1) usingBlock: ^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) {
if (CGRectContainsPoint(usedRect, p)) {
touchingLink = YES;
*stop = YES;
}
}];
return (touchingLink) ? self : nil;
}
return nil;
}
// 選択領域がでないようにする
- (NSArray *)selectionRectsForRange:(UITextRange *)range {
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment