1

When ever in text '@' symbol come like in twitter i need that portion clickable using TTTAttributeLabel. Like example : "hi @test hello text". Only @test part clickable. TTTAttributeLabel class i am using in uitableview cell so that table didSelectedRowAtIndexPath method call when ever user press other part of label means not click on '@test'.

Thanks in advance.

Jay Mehta
  • 1,431
  • 19
  • 40

4 Answers4

2

You can add custom actions to any of the available UILabel replacements that support links using a fake URL scheme that you'll intercept later:

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

Then, in your 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 is a fork of OHAttributedLabel.

If you want a more complex approach, have a look to Nimbus Attributed Label. It support custom links out-of-the-box.

OR

Check out RTLabel

Yuyutsu
  • 2,509
  • 22
  • 38
1

You can use code sample from this question (see highlightMentionsInString: function)

Then add this code to your 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
 }

If you want different behaviour when user/hashtag taped, you'll need to implement separate highlightTagsInLabel: method for #hashtags, with different regexp and different urls (for example tag:tag_string) and call it after highlightMentionsInLabel:

Community
  • 1
  • 1
glyuck
  • 3,367
  • 18
  • 14
0

You can add link with fake url scheme for your UILabel replacements that support links and intercept click on link using delegate.

for example:

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

Then add delegate for your label and implement attributedLabel:didSelectLinkWithURL: method:

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

I do not think that links in your label will conflict with your table view

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
0

This worked for me in Swift (needs to define two extensions) you can found them here

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!))
        }
    }
}
Community
  • 1
  • 1
Pau Ballada
  • 1,491
  • 14
  • 13