Added enumerateLinkRangesContainingPoint:usingBlock:

This commit is contained in:
Claus Höfele
2014-03-14 22:51:42 +01:00
parent 6cf23f78ff
commit 90069e4761
4 changed files with 33 additions and 11 deletions

View File

@@ -35,6 +35,7 @@
[linkTextView addLinkForRange:NSMakeRange(0, 10)];
[linkTextView addLinkForRange:NSMakeRange(100, 5)];
[linkTextView addLinkForRange:NSMakeRange(120, 100)];
linkTextView.linkDelegate = self;
}

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}
}];