null09264 / FRHyperLabel

A subclass of UILabel to allow you adding one or more hyperlinks to the texts
MIT License
232 stars 74 forks source link

Handler not called anymore #30

Closed ghost closed 7 years ago

ghost commented 7 years ago

There must be something broken. When I first used this component at v 1.0.0 everything was fine. Today I checked the function and found it not working anymore. Your own sample code does not work any longer. I updated to 1.0.4 to no avail.

Please check this. I cannot make this NSLog (@"Selected...") appear. Tried on emulator only.

//Step 1: Define a normal attributed string for non-link texts
NSString *string = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis blandit eros, sit amet vehicula justo. Nam at urna neque. Maecenas ac sem eu sem porta dictum nec vel tellus.";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]};

label.attributedText = [[NSAttributedString alloc]initWithString:string attributes:attributes];

//Step 2: Define a selection handler block
void(^handler)(FRHyperLabel *label, NSString *substring) = ^(FRHyperLabel *label, NSString *substring){
    NSLog(@"Selected: %@", substring);
};

//Step 3: Add link substrings
[label setLinksForSubstrings:@[@"Lorem", @"Pellentesque", @"blandit", @"Maecenas"] withLinkHandler:handler];
ghost commented 7 years ago

This project seems to be dead, so I suppose a PR wouldn't help.

I think the core of the problem is described here (answer 2):

https://stackoverflow.com/questions/10325776/touchescancelled-is-called-instead-of-touchesended

I could work around by redirecting the touchesCancelled event to touchesEnded

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    /*
    [UIView transitionWithView:self duration:highLightAnimationTime options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.attributedText = self.backupAttributedText;
    } completion:nil];
    [super touchesCancelled:touches withEvent:event];
     */
    [self touchesEnded:touches withEvent:event];
}

By that change I cannot "undo" a touch to a hyperlink any longer, but the hyperlink at least is working all the time.