无法检测到使用正则表达式HTTP链接(Unable to detect http links usi

2019-10-30 01:10发布

我使用TTTAttributedLabel检测我的超文本链接,这样我可以让他们点击。 我的代码不能正常工作,并链接无法点击。 事实上,一切都点击。 我只想网址为不可点击。

这里是正则表达式:

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

Answer 1:

为什么不干脆: myTextView.dataDetectorTypes = UIDataDetectorTypeLink;



文章来源: Unable to detect http links using Regex