Added properties to extend tap area

This commit is contained in:
choefele
2014-04-14 20:59:12 +02:00
parent 349c84f060
commit edc9be625d
3 changed files with 58 additions and 0 deletions

View File

@@ -90,6 +90,53 @@
XCTAssertEqual(blockCalled, 1u);
}
- (void)testEnumerateLinkRangesNotContainingPoint
{
NSRange linkRange = NSMakeRange(0, 10);
NSMutableAttributedString *attributedText = [self.linkTextView.attributedText mutableCopy];
[attributedText addAttribute:CCHLinkAttributeName value:@"http://google.de" range:linkRange];
self.linkTextView.attributedText = attributedText;
__block NSUInteger blockCalled = 0;
[self.linkTextView enumerateLinkRangesContainingLocation:CGPointMake(100, 20) usingBlock:^(NSRange range) {
blockCalled++;
XCTAssertTrue(NSEqualRanges(range, linkRange));
}];
XCTAssertEqual(blockCalled, 0u);
}
- (void)testEnumerateLinkRangesContainingPointExtendedTapAreaX
{
NSRange linkRange = NSMakeRange(0, 10);
NSMutableAttributedString *attributedText = [self.linkTextView.attributedText mutableCopy];
[attributedText addAttribute:CCHLinkAttributeName value:@"http://google.de" range:linkRange];
self.linkTextView.attributedText = attributedText;
self.linkTextView.extendedTapAreaX = 50;
__block NSUInteger blockCalled = 0;
[self.linkTextView enumerateLinkRangesContainingLocation:CGPointMake(100, 20) usingBlock:^(NSRange range) {
blockCalled++;
XCTAssertTrue(NSEqualRanges(range, linkRange));
}];
XCTAssertEqual(blockCalled, 1u);
}
- (void)testEnumerateLinkRangesContainingPointExtendedTapAreaY
{
NSRange linkRange = NSMakeRange(0, 10);
NSMutableAttributedString *attributedText = [self.linkTextView.attributedText mutableCopy];
[attributedText addAttribute:CCHLinkAttributeName value:@"http://google.de" range:linkRange];
self.linkTextView.attributedText = attributedText;
self.linkTextView.extendedTapAreaY = 80;
__block NSUInteger blockCalled = 0;
[self.linkTextView enumerateLinkRangesContainingLocation:CGPointMake(50, 100) usingBlock:^(NSRange range) {
blockCalled++;
XCTAssertTrue(NSEqualRanges(range, linkRange));
}];
XCTAssertEqual(blockCalled, 1u);
}
- (void)testEnumerateLinkRangesContainingPointTwice
{
NSRange linkRange = NSMakeRange(0, 20);

View File

@@ -47,6 +47,11 @@ extern NSString *const CCHLinkAttributeName;
/** The maximum movement of the fingers on the link before the gesture is ignored (default = 10 points). */
@property (nonatomic, assign) CGFloat allowableMovement;
/** Additional tap area left and right of the link text (default: 10 points). */
@property (nonatomic, assign) CGFloat extendedTapAreaX;
/** Additional tap area above and below of the link text (default: 10 points). */
@property (nonatomic, assign) CGFloat extendedTapAreaY;
/** The gesture recognizer used to detect links in this text view. */
@property (nonatomic, strong, readonly) CCHLinkGestureRecognizer *linkGestureRecognizer;

View File

@@ -64,6 +64,9 @@ NSString *const CCHLinkAttributeName = @"CCHLinkAttributeName";
self.linkGestureRecognizer = [[CCHLinkGestureRecognizer alloc] initWithTarget:self action:@selector(linkAction:)];
self.linkGestureRecognizer.delegate = self;
[self addGestureRecognizer:self.linkGestureRecognizer];
self.extendedTapAreaX = 10;
self.extendedTapAreaY = 10;
}
- (id)debugQuickLookObject
@@ -82,6 +85,7 @@ NSString *const CCHLinkAttributeName = @"CCHLinkAttributeName";
[attributedString enumerateAttribute:CCHLinkAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[self enumerateViewRectsForRanges:@[[NSValue valueWithRange:range]] usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
rect = CGRectInset(rect, -self.extendedTapAreaX, -self.extendedTapAreaY);
CGContextFillRect(context, rect);
}];
}
@@ -139,6 +143,8 @@ NSString *const CCHLinkAttributeName = @"CCHLinkAttributeName";
[attributedString enumerateAttribute:CCHLinkAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[self enumerateViewRectsForRanges:@[[NSValue valueWithRange:range]] usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
rect = CGRectInset(rect, -self.extendedTapAreaX, -self.extendedTapAreaY);
if (CGRectContainsPoint(rect, location)) {
found = YES;
*stop = YES;