Last active
August 29, 2015 14:00
-
-
Save fmtonakai/11144531 to your computer and use it in GitHub Desktop.
TextViewのリンクのところだけ反応させてあとはスルーする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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