2015-01-21 73 views
1

當有人在twitter中使用文字「@」符號時,我需要使用TTTAttributeLabel單擊該部分。例如:「hi @test hello text」。 只有@test部分可點擊。我在uitableview單元格中使用的TTTAttributeLabel類,以便在用戶按下標籤的其他部分時調用table didSelectedRowAtIndexPath方法意味着不要單擊'@test'。如何將TTTAttributedLabel與'@'和'#'一起使用?

在此先感謝。

回答

1

您可以使用代碼示例從this question(見highlightMentionsInString:功能)

然後將此代碼添加到您的tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     MyCell *cell; 
     // configure your cell 
     // ... 
     cell.attributedLabel.delegate = self; 
     cell.attributedLabel.userInteractionEnabled = YES; 
     cell.attributedLabel.text = someText; 
     [self highlightMentionsInLabel:cell.attributedLabel] 
     return cell; 
} 

- (void)highlightMentionsInLabel:(TTTAttributedLabel *)attributedLabel { 
     NSString *text = attributedLabel.text; 
     NSRegularExpression *mentionExpression = [NSRegularExpression regularExpressionWithPattern:@"(?:^|\\s)(@\\w+)" options:NO error:nil]; 
     // and so on, use code from question I linked above 
     // ... 
} 
... 

# pragma mark - TTTAttributedLabelDelegate 

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { 
    // your code here to handle `user:username` links 
} 

如果您想在用戶/包括hashtag錄播不同的行爲,你需要針對#hashtags實施單獨的highlightTagsInLabel:方法,使用不同的正則表達式和不同的網址(例如tag:tag_string)並在之後調用highlightMentionsInLabel:

+0

這個正則表達式有錯誤,像'@ abc @ abcde @ ab' – 2015-09-03 18:28:19

0

您可以爲支持鏈接的UILabel替換添加包含假url地址的鏈接,並攔截使用委託鏈接點擊鏈接。

例如:

TTTAttributedLabel *label = <# create the label here #>; 
NSString *labelText = @"My name is @test."; 
label.text = labelText; 
NSRange r = [labelText rangeOfString:@"Learn more"]; 
// here you can add some params to your url 
[label addLinkToURL:[NSURL URLWithString:@"your-fake-scheeme://say-hello"] withRange:r]; 

然後爲你添加標籤代表和實現attributedLabel:didSelectLinkWithURL:方法:

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { 
    if ([[url scheme] hasPrefix:@"your-fake-scheeme"]) { 
     if ([[url host] hasPrefix:@"say-hello"]) { 
      //parse your url and extract params to do something 
     } 
    } else { 
     // if it is a regular link open it in browser 
     [[UIApplication sharedApplication] openURL:url]; 
    } 
} 

我不認爲在你的標籤的鏈接將與您的表視圖衝突

2

您可以將自定義操作添加到任何可用的UILabel替換項中,該替換項支持使用假冒的鏈接URL方案日後會攔截:

TTTAttributedLabel *tttLabel = <# create the label here #>; 
NSString *labelText = @"Lost? Learn more."; 
tttLabel.text = labelText; 
NSRange r = [labelText rangeOfString:@"Learn more"]; 
[tttLabel addLinkToURL:[NSURL URLWithString:@"action://show-help"] withRange:r]; 

然後,在你TTTAttributedLabelDelegate

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { 
    if ([[url scheme] hasPrefix:@"action"]) { 
     if ([[url host] hasPrefix:@"show-help"]) { 
      /* load help screen */ 
     } else if ([[url host] hasPrefix:@"show-settings"]) { 
      /* load settings screen */ 
     } 
    } else { 
     /* deal with http links here */ 
    } 
} 

TTTAttributedLabel是OHAttributedLabel的一個分支。

如果您想要更復雜的方法,請看Nimbus Attributed Label。它支持開箱即用的自定義鏈接。

OR

退房RTLabel

0

這斯威夫特工作對我來說(需要定義兩個分機),你可以發現他們在這裏

NSRange <-> Range extensions

func highlightMentionsInLabel(attributedLabel:TTTAttributedLabel){ 

    let textObject = attributedLabel.text 

    var mentionExpression:NSRegularExpression? 

    do { 
     mentionExpression = try NSRegularExpression(pattern: "(?:^|\\s)(@\\w+)", options: NSRegularExpressionOptions.CaseInsensitive) 

    }catch{ 
     print("error:\(error)") 
     return 
    } 

    if let text = textObject, let expression = mentionExpression{ 

     let matches: [NSTextCheckingResult] = expression.matchesInString(text, options: NSMatchingOptions.init(rawValue: 0), range: NSMakeRange(0, text.characters.count)) 
     for match: NSTextCheckingResult in matches { 
      let matchRange: NSRange = match.rangeAtIndex(1) 

      let swiftRange = text.rangeFromNSRange(matchRange)! 

      let mentionString: String = text.substringWithRange(swiftRange) 

      let linkRange = text.rangeOfString(mentionString) 

      let index:String.Index = mentionString.startIndex.advancedBy(1) 
      let user = mentionString.substringFromIndex(index) 
      let linkURLString: String = "user:\(user)" 
      attributedLabel.addLinkToURL(NSURL(string: linkURLString)!, withRange: linkURLString.NSRangeFromRange(linkRange!)) 
     } 
    } 
} 
相關問題