Added enumerateViewRectsForRanges:usingBlock:

This commit is contained in:
Claus Höfele
2014-03-14 22:35:44 +01:00
parent 8161e0f451
commit 6cf23f78ff
3 changed files with 92 additions and 1 deletions

View File

@@ -39,7 +39,8 @@
{
[super setUp];
self.linkTextView = [[CCHLinkTextView alloc] init];
CGRect rect = CGRectMake(0, 0, 100, 100);
self.linkTextView = [[CCHLinkTextView alloc] initWithFrame:rect];
self.linkTextView.text = @"012345678901234567890123456789";
}
@@ -71,5 +72,35 @@
XCTAssertEqual(blockCalled, 0u);
}
- (void)testEnumerateViewRectsForRangesOnce
{
NSValue *rangeAsValue = [NSValue valueWithRange:NSMakeRange(0, 10)];
__block NSUInteger blockCalled = 0;
[self.linkTextView enumerateViewRectsForRanges:@[rangeAsValue] usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
blockCalled++;
}];
XCTAssertEqual(blockCalled, 1);
}
- (void)testEnumerateViewRectsForRangesTwice
{
NSValue *rangeAsValue = [NSValue valueWithRange:NSMakeRange(0, 20)];
__block NSUInteger blockCalled = 0;
[self.linkTextView enumerateViewRectsForRanges:@[rangeAsValue] usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
blockCalled++;
}];
XCTAssertEqual(blockCalled, 2);
}
- (void)testEnumerateViewRectsForRangesTwiceStopped
{
NSValue *rangeAsValue = [NSValue valueWithRange:NSMakeRange(0, 20)];
__block NSUInteger blockCalled = 0;
[self.linkTextView enumerateViewRectsForRanges:@[rangeAsValue] usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
blockCalled++;
*stop = YES;
}];
XCTAssertEqual(blockCalled, 1);
}
@end

View File

@@ -40,5 +40,6 @@
- (void)addLinkForRange:(NSRange)range;
//- (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;
@end

View File

@@ -30,6 +30,8 @@
#import "CCHLinkTextViewDelegate.h"
#import "CCHLinkGestureRecognizer.h"
#define DEBUG_COLOR [UIColor colorWithWhite:0 alpha:0.3]
@interface CCHLinkTextView () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) NSMutableArray *linkRanges;
@@ -76,6 +78,63 @@
[self addAttributes:self.linkTextAttributes range:range];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
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);
}];
}
UIGraphicsPopContext();
}
- (BOOL)enumerateLinkRangesContainingPoint:(CGPoint)point usingBlock:(void (^)(NSRange range))block
{
if (!block) {
return NO;
}
__block BOOL found = NO;
[self enumerateViewRectsForRanges:self.linkRanges usingBlock:^(CGRect rect, NSRange range, BOOL *stop) {
if (CGRectContainsPoint(rect, point)) {
found = YES;
block(range);
}
}];
return found;
}
- (void)enumerateViewRectsForRanges:(NSArray *)ranges usingBlock:(void (^)(CGRect rect, NSRange range, BOOL *stop))block
{
if (!block) {
return;
}
for (NSValue *rangeAsValue in ranges) {
NSRange range = rangeAsValue.rangeValue;
NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:range 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;
block(rect, range, stop);
}];
}
}
- (BOOL)enumerateLinkRangesIncludingCharacterIndex:(NSUInteger)characterIndex usingBlock:(void (^)(NSRange range))block
{
if (!block) {