mirror of
https://github.com/zhigang1992/CCHLinkTextView.git
synced 2026-04-24 04:04:59 +08:00
Added properties to extend tap area
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user