hsusmita / ResponsiveLabel

MIT License
218 stars 36 forks source link

Adding multiple detection like email, userhandler and url doesn't work #49

Open Chirag89-JackSparrow opened 6 years ago

Chirag89-JackSparrow commented 6 years ago

I add email detection, url detection and userhandler detection on same label but its not working properly. When i add chirag@y.com its take @y as a userhandler detection part and chirag & .com as a url detection part. So it's not detect chirag@y.com as email part.

NSString *emailRegexString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSError *error;
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:emailRegexString
                                                                     options:0
                                                                       error:&error];
    PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:regex withSearchType:PatternSearchTypeAll
                                                      withPatternAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
    [self.postDetailsL enablePatternDetection:descriptor];

PatternTapResponder userHandleTapAction = ^(NSString *tappedString){
        if ([self.delegate respondsToSelector:@selector(feedCell:didTapOnUserHandle:)]) {
            [self.delegate feedCell:self didTapOnUserHandle:tappedString];
        }
    };
    [self.postDetailsL enableUserHandleDetectionWithAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor], RLTapResponderAttributeName:userHandleTapAction}];

PatternTapResponder URLTapAction = ^(NSString *tappedString){
        if ([self.delegate respondsToSelector:@selector(feedNoMediaCell:didTapOnURLHandle:)]) {
            [self.delegate feedNoMediaCell:self didTapOnURLHandle:tappedString];
        }
    };
    [self.postDetailsL enableURLDetectionWithAttributes:@{NSForegroundColorAttributeName:[UIColor TARedesDisplayColor], NSUnderlineStyleAttributeName:[NSNumber numberWithInt:1], RLTapResponderAttributeName:URLTapAction}];
Chirag89-JackSparrow commented 6 years ago

@hsusmita @thelastjedi @georgesteiner @andreyoshev Did you check the above issue ?

hsusmita commented 6 years ago

@Chirag89-JackSparrow I am currently looking into this issue. Can you tell me if it works if you add detection for email only?

Chirag89-JackSparrow commented 6 years ago

Actually I resolve issue by yesterday by changing regular expressions. Thanks for responding @hsusmita . The problem is, if there is no space before @, still it consider as user tag, and link portion so I change regular expression and of hashtag, user tag and link and implement in methods.

static NSString *kRegexStringForHashTag = @"(?<!\\w)(#(?:[a-z0-9A-Z].*?|\\d+[a-z0-9A-Z]+.*?))\\b";
static NSString *kRegexStringForUserHandle = @"(?<!\\w)(@(?:[a-z0-9A-Z].*?|\\d+[a-z0-9A-Z]+.*?))\\b";
static NSString *kRegexStringForURLLinkHandle = @"((((https?|ftp|gopher|telnet|file):((//)|(\\\\)))|((www|ftp)\\.))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";

Changes in methods

- (void)enableURLDetectionWithAttributes:(NSDictionary*)dictionary {
    NSError *error;
    NSRegularExpression    *regex = [[NSRegularExpression alloc]initWithPattern:kRegexStringForURLLinkHandle options:0 error:&error];
    PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:regex
                                                             withSearchType:PatternSearchTypeAll
                                                      withPatternAttributes:dictionary];
    [self enablePatternDetection:descriptor];
}
- (void)disableURLDetection {
    [self disablePatternDetection:[self.patternDescriptorDictionary objectForKey:kRegexStringForURLLinkHandle]];
}

- (void)enableHashTagDetectionWithAttributes:(NSDictionary*)dictionary {
    NSError *error;
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:kRegexStringForHashTag options:0 error:&error];
    PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:regex
                                                             withSearchType:PatternSearchTypeAll
                                                      withPatternAttributes:dictionary];
    [self enablePatternDetection:descriptor];
}

- (void)disableHashTagDetection {
    [self disablePatternDetection:[self.patternDescriptorDictionary objectForKey:kRegexStringForHashTag]];
}

- (void)enableUserHandleDetectionWithAttributes:(NSDictionary*)dictionary {
    NSError *error;
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:kRegexStringForUserHandle
                                                                     options:0
                                                                       error:&error];
    PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:regex
                                                             withSearchType:PatternSearchTypeAll
                                                      withPatternAttributes:dictionary];
    [self enablePatternDetection:descriptor];
}

- (void)disableUserHandleDetection {
    [self disablePatternDetection:[self.patternDescriptorDictionary objectForKey:kRegexStringForUserHandle]];
}

So now email detection is work perfect for me.

NSString *emailRegexString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSError *error;
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:emailRegexString
                                                                     options:0
                                                                       error:&error];
    PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:regex withSearchType:PatternSearchTypeAll
                                                      withPatternAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
    [self.postDetailsL enablePatternDetection:descriptor];
hsusmita commented 6 years ago

@Chirag89-JackSparrow Cool! Thanks for updating. 👍

Chirag89-JackSparrow commented 6 years ago

You can help with me for this issue @hsusmita @thelastjedi @georgesteiner @andreyoshev

When I add user tag and hashtag detection in custom tableview cell label and I scroll the tableview, sometimes user tag and hashtag is not detected. But when I scroll up its come as detected. Didn't found why it happens.

I have attach screen shots. Please check and let me know.

ios1 ios2

hsusmita commented 6 years ago

@Chirag89-JackSparrow I will look into this.

Chirag89-JackSparrow commented 6 years ago

@hsusmita Let me know as soon as possible. Thanks for responding.

Chirag89-JackSparrow commented 6 years ago

@hsusmita Any update on the above issue as we discussed ? I can't able to resolve this from my end.

hsusmita commented 6 years ago

@Chirag89-JackSparrow I didn't get time to update this. However, this library is breaking for ios 12. Can you try the swift version SwiftResponsiveLabel?