mirror of
https://github.com/zhigang1992/CCHLinkTextView.git
synced 2026-04-23 19:50:07 +08:00
Added enumerateViewRectsForRanges:usingBlock:
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user