Unable to detect http links using Regex

回眸只為那壹抹淺笑 提交于 2020-04-18 08:41:27

问题


I am using TTTAttributedLabel to detect hyperlinks in my text so that I can make them clickable. My code does not work, and links aren't clickable. In fact, everything is clickable. I only want URL's to be clickable.

here is the regex:

static NSRegularExpression *websiteRegularExpression;
static inline NSRegularExpression * WebsiteRegularExpression() {
    if (!websiteRegularExpression) {
        websiteRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]" 
                                                                        options:NSRegularExpressionCaseInsensitive 
                                                                          error:nil];
    }

    return websiteRegularExpression;
}

Here is the implementation

-(void)setBodyText
{
    __block NSRegularExpression *regexp = nil;   
    NSString* labelText = [[message valueForKey:@"body"]gtm_stringByUnescapingFromHTML]; //@"http://www.google.com is a cool website";
    [self.bodyLabel setText:labelText afterInheritingLabelAttributesAndConfiguringWithBlock:^NSAttributedString *(NSMutableAttributedString *mutableAttributedString) {

        NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
       /* regexp = WebsiteRegularExpression ();
        NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange];
        UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:18.0]; 
        CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
        if (boldFont) {
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:nameRange];
            CFRelease(boldFont);
        }

        if (nameRange.location != NSNotFound)
            [mutableAttributedString replaceCharactersInRange:nameRange withString:[[[mutableAttributedString string] substringWithRange:nameRange] uppercaseString]];
        return mutableAttributedString;
        */

        regexp = WebsiteRegularExpression();
        [regexp enumerateMatchesInString:[mutableAttributedString string] options:0 range:stringRange usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {            
            UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:18.0];
            CTFontRef italicFont = CTFontCreateWithName((CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
            if (italicFont) {
                [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)italicFont range:result.range];
                CFRelease(italicFont);

                [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:result.range];
            }
        }];

        return mutableAttributedString;

    }];


    regexp = WebsiteRegularExpression();
    NSRange linkRange = [regexp rangeOfFirstMatchInString:labelText options:0 range:NSMakeRange(0, [labelText length])];
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    [self.bodyLabel addLinkToURL:url withRange:linkRange];

    [Utils alignLabelWithTop:bodyLabel];
}

回答1:


Why not just: myTextView.dataDetectorTypes = UIDataDetectorTypeLink;?



来源:https://stackoverflow.com/questions/7002645/unable-to-detect-http-links-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!