kishikawakatsumi / SECoreTextView

SECoreTextView is multi style text view.
MIT License
943 stars 143 forks source link

Data detectors? #34

Closed xhzengAIB closed 10 years ago

xhzengAIB commented 10 years ago

Hi! @kishikawakatsumi Thank you for your great contribution.

Data detectors recognizes phone numbers, links, dates, etc????

Jack

hahv commented 10 years ago

Add this property in SETextView. @property (nonatomic) NSTextCheckingType dataDetectorTypes; In SETextLayout, change detectLinks function to this:

- (void)detectLinks:(NSTextCheckingType) checkingType
{
    NSMutableArray *links = [[NSMutableArray alloc] init];

    NSUInteger length = self.attributedString.length;
    [self.attributedString enumerateAttribute:NSLinkAttributeName
                                      inRange:NSMakeRange(0, length)
                                      options:0
                                   usingBlock:^(id value, NSRange range, BOOL *stop)
     {
         if (value) {
             NSString *linkText = [self.attributedString.string substringWithRange:range];
             SELinkText *link = [[SELinkText alloc] initWithText:linkText object:value range:range];
             link.textType = NSTextCheckingTypeLink;
             [links addObject:link];
         }
     }];

    NSError* error = nil;
    NSDataDetector* linkDetector = [NSDataDetector dataDetectorWithTypes:checkingType
                                                                   error:&error];
    NSRange range = NSMakeRange(0,length);

    NSArray * dectectedLinks =  [linkDetector matchesInString:self.attributedString.string options:0 range:range];

    for (NSUInteger i = 0; i < [dectectedLinks count]; i++) {
        NSTextCheckingResult* result = [dectectedLinks objectAtIndex:i];

        NSString *linkText = [self.attributedString.string substringWithRange:result.range];
        SELinkText *link = [[SELinkText alloc] initWithText:linkText object:nil range:result.range];
        link.textType = result.resultType;
        [links addObject:link];
    }

    _links = links.copy;
}

Job done!

Solution from https://github.com/mattt/TTTAttributedLabel

xhzengAIB commented 10 years ago

@hahaha12a2 Ok! Thanks you for your help.