AliSoftware / OHAttributedLabel

UILabel that supports NSAttributedString

Home Page:https://github.com/AliSoftware/OHAttributedLabel/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: activate links on touchBegin instead of touchEnd.

Infiniverse opened this issue · comments

Hi there,

I've got a need to have an embedded link activated when it get's touchBegin'd, instead of at touchEnd time.

Can you consider adding this patch which supports this as an optional feature please?

Thanks,
Joe

--- a/DrDotMaths/iOSPrototype/Libraries/OHAttributedLabel/OHAttributedLabel/Source/OHAttributedLabel.h
+++ b/DrDotMaths/iOSPrototype/Libraries/OHAttributedLabel/OHAttributedLabel/Source/OHAttributedLabel.h
@@ -105,6 +105,8 @@ typedef NS_OPTIONS(int32_t, OHBoldStyleTrait) {

//! If YES, pointInside will only return YES if the touch is on a link. If NO, pointInside will always return YES (Defaults to YES)
@Property(nonatomic, assign) BOOL onlyCatchTouchesOnLinks;
+//! If YES, any touched links are process on touchBegin, otherwise on touchEnd (Defaults to NO)
+@Property(nonatomic, assign) BOOL catchTouchesOnLinksOnTouchBegan;
//! The delegate that gets informed when a link is touched and gives the opportunity to catch it
@Property(nonatomic, assign) IBOutlet id delegate;

diff --git a/DrDotMaths/iOSPrototype/Libraries/OHAttributedLabel/OHAttributedLabel/Source/OHAttributedLabel.m b/DrDotMaths/iOSPrototype/Libraries/OHAttributedLabel/OHAttributedLabel/Source/OHAttributedLabel.m
index 905e52d..73d16c4 100755
--- a/DrDotMaths/iOSPrototype/Libraries/OHAttributedLabel/OHAttributedLabel/Source/OHAttributedLabel.m
+++ b/DrDotMaths/iOSPrototype/Libraries/OHAttributedLabel/OHAttributedLabel/Source/OHAttributedLabel.m
@@ -449,7 +449,10 @@ NSDataDetector* sharedReusableDataDetector(NSTextCheckingTypes types)

    self.activeLink = [self linkAtPoint:pt];
    _touchStartPoint = pt;
  •   if (_catchTouchesOnLinksOnTouchBegan) {
    
  •           [self processLinkAt:pt];
    
  •   }
    // we're using activeLink to draw a highlight in -drawRect:
    [self setNeedsDisplay];
    

    }
    @@ -458,9 +461,15 @@ NSDataDetector* sharedReusableDataDetector(NSTextCheckingTypes types)
    {
    UITouch* touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self];

  •   if (!_catchTouchesOnLinksOnTouchBegan) {
    
  •           [self processLinkAt:pt];
    
  •   }
    
  •   [self setNeedsDisplay];
    

    +}
    +
    +- (void)processLinkAt:(CGPoint)pt {
    NSTextCheckingResult *linkAtTouchesEnded = [self linkAtPoint:pt];

    BOOL closeToStart = (abs(_touchStartPoint.x - pt.x) < 10 && abs(_touchStartPoint.y - pt.y) < 10);
    
    // we can check on equality of the ranges themselfes since the data detectors create new results
    

    @@ -476,9 +485,8 @@ NSDataDetector* sharedReusableDataDetector(NSTextCheckingTypes types)
    [[UIApplication sharedApplication] openURL:linkToOpen.extendedURL];
    }
    }

    self.activeLink = nil;
    
  •   [self setNeedsDisplay];
    

    }

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

Thx for the suggestion, but please submit a GitHub Pull Request instead of posting this unreadable diff code that is hardly integratable as is. (Note: submitting a Pull Request will automatically open a new associated issue, so I'm closing this one)