无法使用正则表达式检测http链接 [英] Unable to detect http links using Regex

查看:199
本文介绍了无法使用正则表达式检测http链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TTTAttributedLabel来检测文本中的超链接,以便可以将其单击。我的代码无效,链接也无法点击。实际上,一切都是可点击的。我只希望URL是可单击的。

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.

这是正则表达式:

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;
}

这里是实现方式

-(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];
}


推荐答案

为什么不只是: myTextView.dataDetectorTypes = UIDataDetectorTypeLink;

这篇关于无法使用正则表达式检测http链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆