mirror of
https://github.com/zhigang1992/CCHLinkTextView.git
synced 2026-04-23 11:46:51 +08:00
Added enumerateLinkRangesContainingPoint:usingBlock:
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
|
||||
[linkTextView addLinkForRange:NSMakeRange(0, 10)];
|
||||
[linkTextView addLinkForRange:NSMakeRange(100, 5)];
|
||||
[linkTextView addLinkForRange:NSMakeRange(120, 100)];
|
||||
linkTextView.linkDelegate = self;
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@
|
||||
XCTAssertEqual(blockCalled, 0u);
|
||||
}
|
||||
|
||||
- (void)testEnumerateViewRectsForRangesOnce
|
||||
- (void)testEnumerateViewRectsForRanges
|
||||
{
|
||||
NSValue *rangeAsValue = [NSValue valueWithRange:NSMakeRange(0, 10)];
|
||||
__block NSUInteger blockCalled = 0;
|
||||
@@ -103,4 +103,30 @@
|
||||
XCTAssertEqual(blockCalled, 1);
|
||||
}
|
||||
|
||||
- (void)testEnumerateLinkRangesContainingPoint
|
||||
{
|
||||
NSRange linkRange = NSMakeRange(0, 10);
|
||||
[self.linkTextView addLinkForRange:linkRange];
|
||||
|
||||
__block NSUInteger blockCalled = 0;
|
||||
[self.linkTextView enumerateLinkRangesContainingPoint:CGPointMake(50, 20) usingBlock:^(NSRange range) {
|
||||
blockCalled++;
|
||||
XCTAssertTrue(NSEqualRanges(range, linkRange));
|
||||
}];
|
||||
XCTAssertEqual(blockCalled, 1);
|
||||
}
|
||||
|
||||
- (void)testEnumerateLinkRangesContainingPointTwice
|
||||
{
|
||||
NSRange linkRange = NSMakeRange(0, 20);
|
||||
[self.linkTextView addLinkForRange:linkRange];
|
||||
|
||||
__block NSUInteger blockCalled = 0;
|
||||
[self.linkTextView enumerateLinkRangesContainingPoint:CGPointMake(50, 20) usingBlock:^(NSRange range) {
|
||||
blockCalled++;
|
||||
XCTAssertTrue(NSEqualRanges(range, linkRange));
|
||||
}];
|
||||
XCTAssertEqual(blockCalled, 1);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -41,5 +41,6 @@
|
||||
//- (void)removeLinkForRange:(NSRange)range;
|
||||
- (BOOL)enumerateLinkRangesIncludingCharacterIndex:(NSUInteger)characterIndex usingBlock:(void (^)(NSRange range))block;
|
||||
- (void)enumerateViewRectsForRanges:(NSArray *)ranges usingBlock:(void (^)(CGRect rect, NSRange range, BOOL *stop))block;
|
||||
- (BOOL)enumerateLinkRangesContainingPoint:(CGPoint)point usingBlock:(void (^)(NSRange range))block;
|
||||
|
||||
@end
|
||||
|
||||
@@ -86,16 +86,9 @@
|
||||
UIGraphicsPushContext(context);
|
||||
|
||||
CGContextSetFillColorWithColor(context, DEBUG_COLOR.CGColor);
|
||||
|
||||
for (NSValue *rangeAsValue in self.linkRanges) {
|
||||
NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:rangeAsValue.rangeValue actualCharacterRange:NULL];
|
||||
[self.layoutManager enumerateEnclosingRectsForGlyphRange:glyphRange withinSelectedGlyphRange:NSMakeRange(NSNotFound, 0) inTextContainer:self.textContainer usingBlock:^(CGRect rect, BOOL *stop) {
|
||||
rect.origin.x += self.textContainerInset.left;
|
||||
rect.origin.y += self.textContainerInset.top;
|
||||
|
||||
CGContextFillRect(context, rect);
|
||||
}];
|
||||
}
|
||||
[self enumerateViewRectsForRanges:self.linkRanges usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
|
||||
CGContextFillRect(context, rect);
|
||||
}];
|
||||
|
||||
UIGraphicsPopContext();
|
||||
}
|
||||
@@ -110,6 +103,7 @@
|
||||
[self enumerateViewRectsForRanges:self.linkRanges usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
|
||||
if (CGRectContainsPoint(rect, point)) {
|
||||
found = YES;
|
||||
*stop = YES;
|
||||
block(range);
|
||||
}
|
||||
}];
|
||||
|
||||
Reference in New Issue
Block a user