Skip to content

Commit

Permalink
[Links] Block-based tap and long press handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jhersh committed Feb 28, 2015
1 parent b62d8ca commit c34d4e0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Example/Espressos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_GENERATE_TEST_COVERAGE_FILES = YES;
GCC_INSTRUMENT_PROGRAM_FLOW_ARCS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = DEBUG;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
Expand Down Expand Up @@ -520,6 +522,8 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_GENERATE_TEST_COVERAGE_FILES = YES;
GCC_INSTRUMENT_PROGRAM_FLOW_ARCS = YES;
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
GCC_VERSION = "";
Expand Down
40 changes: 40 additions & 0 deletions Example/TTTAttributedLabelTests/TTTAttributedLabelTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,46 @@ - (void)testLongTextCheckingPressCallsDelegate {
[TTTDelegateMock verify];
}

- (void)testLinkPressCallsLinkBlock {
label.text = TTTAttributedTestString();
TTTSizeAttributedLabel(label);

__block BOOL didCallTapBlock = NO;
__block BOOL didCallLongPressBlock = NO;

NSTextCheckingResult *result = [NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, 4) URL:testURL];
TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label
textCheckingResult:result];

__weak typeof (link) weakLink = link;
__weak typeof (result) weakResult = result;
link.linkTapBlock = ^(TTTAttributedLabel *aLabel, TTTAttributedLabelLink *aLink) {
didCallTapBlock = YES;

expect(aLabel).to.equal(label);
expect(aLink).to.equal(weakLink);
expect(aLink.result).to.equal(weakResult);
};

link.linkLongPressBlock = ^(__unused TTTAttributedLabel *aLabel, __unused TTTAttributedLabelLink *aLink) {
didCallLongPressBlock = YES;
};

[label addLink:link];

TTTSimulateTapOnLabelAtPoint(label, CGPointMake(5, 3));

expect(didCallTapBlock).will.beTruthy();
expect(didCallLongPressBlock).will.beFalsy();

didCallTapBlock = NO;

TTTSimulateLongPressOnLabelAtPointWithDuration(label, CGPointMake(5, 5), 0.6f);

expect(didCallTapBlock).will.beFalsy();
expect(didCallLongPressBlock).will.beTruthy();
}

#pragma mark - UIPasteboard

- (void)testCopyingLabelText {
Expand Down
16 changes: 16 additions & 0 deletions TTTAttributedLabel/TTTAttributedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ didLongPressLinkWithTextCheckingResult:(NSTextCheckingResult *)result

@interface TTTAttributedLabelLink : NSObject <NSCoding>

typedef void (^TTTAttributedLabelLinkBlock) (TTTAttributedLabel *, TTTAttributedLabelLink *);

/**
An `NSTextCheckingResult` representing the link's location and type.
*/
Expand All @@ -641,6 +643,20 @@ didLongPressLinkWithTextCheckingResult:(NSTextCheckingResult *)result
*/
@property (nonatomic, copy) NSString *accessibilityValue;

/**
A block called when this link is tapped.
If non-nil, tapping on this link will call this block instead of the
@c TTTAttributedLabelDelegate tap methods, which will not be called for this link.
*/
@property (nonatomic, copy) TTTAttributedLabelLinkBlock linkTapBlock;

/**
A block called when this link is long-pressed.
If non-nil, long pressing on this link will call this block instead of the
@c TTTAttributedLabelDelegate long press methods, which will not be called for this link.
*/
@property (nonatomic, copy) TTTAttributedLabelLinkBlock linkLongPressBlock;

/**
Initializes a link using the attribute dictionaries specified.
Expand Down
17 changes: 15 additions & 2 deletions TTTAttributedLabel/TTTAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,12 @@ - (void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
if (self.activeLink) {
if (self.activeLink.linkTapBlock) {
self.activeLink.linkTapBlock(self, self.activeLink);
self.activeLink = nil;
return;
}

NSTextCheckingResult *result = self.activeLink.result;
self.activeLink = nil;

Expand Down Expand Up @@ -1573,9 +1579,16 @@ - (void)longPressGestureDidFire:(UILongPressGestureRecognizer *)sender {
switch (sender.state) {
case UIGestureRecognizerStateBegan: {
CGPoint touchPoint = [sender locationInView:self];
NSTextCheckingResult *result = [self linkAtPoint:touchPoint].result;
TTTAttributedLabelLink *link = [self linkAtPoint:touchPoint];

if (result) {
if (link) {
if (link.linkLongPressBlock) {
link.linkLongPressBlock(self, link);
return;
}

NSTextCheckingResult *result = link.result;

switch (result.resultType) {
case NSTextCheckingTypeLink:
if ([self.delegate respondsToSelector:@selector(attributedLabel:didLongPressLinkWithURL:atPoint:)]) {
Expand Down

0 comments on commit c34d4e0

Please sign in to comment.