Added method to enumerate link ranges and added unit test

This commit is contained in:
Claus Höfele
2014-03-05 17:50:13 +01:00
parent ebab4bc6c1
commit 9c5bd2fe57
5 changed files with 90 additions and 54 deletions

View File

@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
623AC49518C11BAD000962A0 /* CCHLinkTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = 623AC49418C11BAD000962A0 /* CCHLinkTextView.m */; };
626F837918C75FBC004FEABB /* CCHLinkTextViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 626F837818C75FBC004FEABB /* CCHLinkTextViewTests.m */; };
62F55CC118C1180200A7E1CC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC018C1180200A7E1CC /* Foundation.framework */; };
62F55CC318C1180200A7E1CC /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC218C1180200A7E1CC /* CoreGraphics.framework */; };
62F55CC518C1180200A7E1CC /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC418C1180200A7E1CC /* UIKit.framework */; };
@@ -21,7 +22,6 @@
62F55CE118C1180200A7E1CC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC018C1180200A7E1CC /* Foundation.framework */; };
62F55CE218C1180200A7E1CC /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC418C1180200A7E1CC /* UIKit.framework */; };
62F55CEA18C1180200A7E1CC /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 62F55CE818C1180200A7E1CC /* InfoPlist.strings */; };
62F55CEC18C1180200A7E1CC /* CCHLinkTextView_ExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F55CEB18C1180200A7E1CC /* CCHLinkTextView_ExampleTests.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -38,6 +38,7 @@
623AC49318C11BAD000962A0 /* CCHLinkTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCHLinkTextView.h; sourceTree = "<group>"; };
623AC49418C11BAD000962A0 /* CCHLinkTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCHLinkTextView.m; sourceTree = "<group>"; };
623AC49618C11EAA000962A0 /* CCHLinkTextViewDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CCHLinkTextViewDelegate.h; sourceTree = "<group>"; };
626F837818C75FBC004FEABB /* CCHLinkTextViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCHLinkTextViewTests.m; sourceTree = "<group>"; };
62F55CBD18C1180200A7E1CC /* CCHLinkTextView Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "CCHLinkTextView Example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
62F55CC018C1180200A7E1CC /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
62F55CC218C1180200A7E1CC /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
@@ -56,7 +57,6 @@
62F55CDF18C1180200A7E1CC /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
62F55CE718C1180200A7E1CC /* CCHLinkTextView ExampleTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "CCHLinkTextView ExampleTests-Info.plist"; sourceTree = "<group>"; };
62F55CE918C1180200A7E1CC /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
62F55CEB18C1180200A7E1CC /* CCHLinkTextView_ExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CCHLinkTextView_ExampleTests.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -153,7 +153,7 @@
62F55CE518C1180200A7E1CC /* CCHLinkTextView ExampleTests */ = {
isa = PBXGroup;
children = (
62F55CEB18C1180200A7E1CC /* CCHLinkTextView_ExampleTests.m */,
626F837818C75FBC004FEABB /* CCHLinkTextViewTests.m */,
62F55CE618C1180200A7E1CC /* Supporting Files */,
);
path = "CCHLinkTextView ExampleTests";
@@ -276,7 +276,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
62F55CEC18C1180200A7E1CC /* CCHLinkTextView_ExampleTests.m in Sources */,
626F837918C75FBC004FEABB /* CCHLinkTextViewTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -0,0 +1,58 @@
//
// CCHLinkTextViewTests.m
// CCHLinkTextView Example
//
// Created by Hoefele, Claus on 05.03.14.
// Copyright (c) 2014 Claus Höfele. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "CCHLinkTextView.h"
@interface CCHLinkTextViewTests : XCTestCase
@property (nonatomic, strong) CCHLinkTextView *linkTextView;
@end
@implementation CCHLinkTextViewTests
- (void)setUp
{
[super setUp];
self.linkTextView = [[CCHLinkTextView alloc] init];
self.linkTextView.text = @"012345678901234567890123456789";
}
- (void)testLinkRangeFound
{
NSRange range = NSMakeRange(3, 4);
[self.linkTextView addLinkForRange:range];
__block NSUInteger blockCalled = 0;
BOOL linkFound = [self.linkTextView enumerateLinkRangesIncludingCharacterIndex:4 usingBlock:^(NSRange range) {
blockCalled++;
}];
XCTAssertTrue(linkFound);
XCTAssertEqual(blockCalled, 1u);
}
- (void)testLinkRangeNotFound
{
NSRange range = NSMakeRange(3, 4);
[self.linkTextView addLinkForRange:range];
__block NSUInteger blockCalled = 0;
BOOL linkFound = [self.linkTextView enumerateLinkRangesIncludingCharacterIndex:1 usingBlock:^(NSRange range) {
blockCalled++;
}];
XCTAssertFalse(linkFound);
XCTAssertEqual(blockCalled, 0u);
}
@end

View File

@@ -1,34 +0,0 @@
//
// CCHLinkTextView_ExampleTests.m
// CCHLinkTextView ExampleTests
//
// Created by Claus Höfele on 28.02.14.
// Copyright (c) 2014 Claus Höfele. All rights reserved.
//
#import <XCTest/XCTest.h>
@interface CCHLinkTextView_ExampleTests : XCTestCase
@end
@implementation CCHLinkTextView_ExampleTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample
{
XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
}
@end

View File

@@ -15,5 +15,6 @@
@property (nonatomic, weak) id<CCHLinkTextViewDelegate> linkDelegate;
- (void)addLinkForRange:(NSRange)range;
- (BOOL)enumerateLinkRangesIncludingCharacterIndex:(NSUInteger)characterIndex usingBlock:(void (^)(NSRange range))block;
@end

View File

@@ -58,32 +58,24 @@
location.y -= self.textContainerInset.top;
NSUInteger characterIndex = [self.layoutManager characterIndexForPoint:location inTextContainer:self.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];
BOOL linkTapped = NO;
for (NSValue *value in self.linkRanges) {
NSRange range = value.rangeValue;
if (NSLocationInRange(characterIndex, range)) {
linkTapped = YES;
[self linkTappedAtCharacterIndex:characterIndex range:range];
}
}
BOOL linkTapped = [self enumerateLinkRangesIncludingCharacterIndex:characterIndex usingBlock:^(NSRange range) {
[self didTapLinkAtCharacterIndex:characterIndex range:range];
}];
if (!linkTapped) {
[self textViewTapped];
[self linkTextViewDidTap];
}
}
}
- (void)linkTappedAtCharacterIndex:(NSUInteger)characterIndex range:(NSRange)range
- (void)didTapLinkAtCharacterIndex:(NSUInteger)characterIndex range:(NSRange)range
{
// NSDictionary *attributes = @{NSBackgroundColorAttributeName : UIColor.redColor};
// [self updateWithAttributes:attributes range:range];
if ([self.linkDelegate respondsToSelector:@selector(linkTextView:didTapLinkAtCharacterIndex:)]) {
[self.linkDelegate linkTextView:self didTapLinkAtCharacterIndex:characterIndex];
}
}
- (void)textViewTapped
- (void)linkTextViewDidTap
{
if ([self.linkDelegate respondsToSelector:@selector(linkTextViewDidTap:)]) {
[self.linkDelegate linkTextViewDidTap:self];
@@ -95,10 +87,29 @@
[self.linkRanges addObject:[NSValue valueWithRange:range]];
NSDictionary *attributes = @{NSBackgroundColorAttributeName : UIColor.greenColor};
[self updateWithAttributes:attributes range:range];
[self addAttributes:attributes range:range];
}
- (void)updateWithAttributes:(NSDictionary *)attributes range:(NSRange)range
- (BOOL)enumerateLinkRangesIncludingCharacterIndex:(NSUInteger)characterIndex usingBlock:(void (^)(NSRange range))block
{
if (!block) {
return NO;
}
BOOL linkTapped = NO;
for (NSValue *value in self.linkRanges) {
NSRange range = value.rangeValue;
if (NSLocationInRange(characterIndex, range)) {
linkTapped = YES;
block(range);
}
}
return linkTapped;
}
- (void)addAttributes:(NSDictionary *)attributes range:(NSRange)range
{
NSMutableAttributedString *attributedText = [self.attributedText mutableCopy];
[attributedText addAttributes:attributes range:range];