2011-08-09 50 views
0

我正在使用TTTAttributedLabel檢測文本中的超鏈接,以便讓它們可點擊。我的代碼不起作用,鏈接不可點擊。事實上,一切都是可點擊的。我只希望URL是可點擊的。無法使用正則表達式檢測http鏈接

這裏是正則表達式:

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

首先,你說的http,你的正則表達式包含更多的http,其次,嘗試像這樣:'https?://。[^] +' – Anders

回答

5

爲什麼不乾脆:myTextView.dataDetectorTypes = UIDataDetectorTypeLink;

+1

我希望能夠打開自定義視圖控制器中的鏈接我的應用程序,而不是踢用戶到Safari瀏覽器。 –

+0

Right before: NSRange linkRange = [regexp rangeOfFirstMatchInString:labelText options:0 range:NSMakeRange(0,[labelText length])];; 你可以檢查labelText是否已經發布?它看起來像labelText字符串被設置爲autorelease,所以它可能會消失,當你設置範圍,所以範圍成爲一切。 –

+1

使用'dataDetectorTypes'屬性並將'delegate'設置爲您的控制器。然後,在你的控制器中,你可以實現委託方法'attributedLabel:didSelectLinkWithURL:'來做你想做的任何事情。 – mattt