0

我正在使用 TTTAttributedLabel 检测文本中的超链接,以便使它们可点击。我的代码不起作用,链接不可点击。事实上,一切都是可点击的。我只希望 URL 是可点击的。

这是正则表达式:

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

1 回答 1

5

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

于 2011-08-09T20:52:05.610 回答