Skip to content

Commit

Permalink
Add new property alwaysHandleTapTruncationAction to ASTextNode2 and A…
Browse files Browse the repository at this point in the history
…STextNode. (#1520)

If the alwaysHandleTapTruncationAction is YES, the ASTextNode and ASTextNode2 can handle touches on additional attributed message even passthrough is YES.
  • Loading branch information
dirtmelon authored and nguyenhuy committed Jul 30, 2019
1 parent 0568a53 commit a88e3cf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Source/ASTextNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,17 @@ NS_ASSUME_NONNULL_BEGIN

/**
@abstract if YES will not intercept touches for non-link areas of the text. Default is NO.
@discussion If you still want to handle tap truncation action when passthroughNonlinkTouches is YES,
you should set the alwaysHandleTruncationTokenTap to YES.
*/
@property (nonatomic) BOOL passthroughNonlinkTouches;

/**
@abstract Always handle tap truncationAction, even the passthroughNonlinkTouches is YES. Default is NO.
@discussion if this is set to YES, the [ASTextNodeDelegate textNodeTappedTruncationToken:] callback will be called.
*/
@property (nonatomic) BOOL alwaysHandleTruncationTokenTap;

@end

@interface ASTextNode (Unavailable)
Expand Down
20 changes: 19 additions & 1 deletion Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ @implementation ASTextNode {
ASTextNodeHighlightStyle _highlightStyle;
BOOL _longPressCancelsTouches;
BOOL _passthroughNonlinkTouches;
BOOL _alwaysHandleTruncationTokenTap;
}
@dynamic placeholderEnabled;

Expand Down Expand Up @@ -992,11 +993,16 @@ - (UIImage *)placeholderImage
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
ASDisplayNodeAssertMainThread();

ASLockScopeSelf(); // Protect usage of _passthroughNonlinkTouches and _alwaysHandleTruncationTokenTap ivars.

if (!_passthroughNonlinkTouches) {
return [super pointInside:point withEvent:event];
}

if (_alwaysHandleTruncationTokenTap) {
return YES;
}

NSRange range = NSMakeRange(0, 0);
NSString *linkAttributeName = nil;
BOOL inAdditionalTruncationMessage = NO;
Expand Down Expand Up @@ -1132,6 +1138,18 @@ - (BOOL)_pendingTruncationTap
return [_highlightedLinkAttributeName isEqualToString:ASTextNodeTruncationTokenAttributeName];
}

- (BOOL)alwaysHandleTruncationTokenTap
{
ASLockScopeSelf();
return _alwaysHandleTruncationTokenTap;
}

- (void)setAlwaysHandleTruncationTokenTap:(BOOL)alwaysHandleTruncationTokenTap
{
ASLockScopeSelf();
_alwaysHandleTruncationTokenTap = alwaysHandleTruncationTokenTap;
}

#pragma mark - Shadow Properties

- (CGColorRef)shadowColor
Expand Down
8 changes: 8 additions & 0 deletions Source/ASTextNode2.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,17 @@ NS_ASSUME_NONNULL_BEGIN

/**
@abstract if YES will not intercept touches for non-link areas of the text. Default is NO.
@discussion If you still want to handle tap truncation action when passthroughNonlinkTouches is YES,
you should set the alwaysHandleTruncationTokenTap to YES.
*/
@property (nonatomic) BOOL passthroughNonlinkTouches;

/**
@abstract Always handle tap truncationAction, even the passthroughNonlinkTouches is YES. Default is NO.
@discussion if this is set to YES, the [ASTextNodeDelegate textNodeTappedTruncationToken:] callback will be called.
*/
@property (nonatomic) BOOL alwaysHandleTruncationTokenTap;

+ (void)enableDebugging;

#pragma mark - Layout and Sizing
Expand Down
20 changes: 19 additions & 1 deletion Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ @implementation AS_TN2_CLASSNAME {
ASTextNodeHighlightStyle _highlightStyle;
BOOL _longPressCancelsTouches;
BOOL _passthroughNonlinkTouches;
BOOL _alwaysHandleTruncationTokenTap;
}
@dynamic placeholderEnabled;

Expand Down Expand Up @@ -967,10 +968,15 @@ - (UIImage *)placeholderImage
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
ASDisplayNodeAssertMainThread();

ASLockScopeSelf(); // Protect usage of _passthroughNonlinkTouches and _alwaysHandleTruncationTokenTap ivars.

if (!_passthroughNonlinkTouches) {
return [super pointInside:point withEvent:event];
}

if (_alwaysHandleTruncationTokenTap) {
return YES;
}

NSRange range = NSMakeRange(0, 0);
NSString *linkAttributeName = nil;
Expand Down Expand Up @@ -1117,6 +1123,18 @@ - (BOOL)_pendingTruncationTap
return [ASLockedSelf(_highlightedLinkAttributeName) isEqualToString:ASTextNodeTruncationTokenAttributeName];
}

- (BOOL)alwaysHandleTruncationTokenTap
{
ASLockScopeSelf();
return _alwaysHandleTruncationTokenTap;
}

- (void)setAlwaysHandleTruncationTokenTap:(BOOL)alwaysHandleTruncationTokenTap
{
ASLockScopeSelf();
_alwaysHandleTruncationTokenTap = alwaysHandleTruncationTokenTap;
}

#pragma mark - Shadow Properties

/**
Expand Down

0 comments on commit a88e3cf

Please sign in to comment.