TTTAttributedLabel clickable truncation token

2.7k views Asked by At

I have a TTTAttributedLabel and specified a custom attributed truncation token for it:

NSAttributedString *atributedTruncationToken = [[[NSAttributedString alloc]
                                                     initWithString:@" More..."
                                                         attributes:@{
                                                                      NSForegroundColorAttributeName : [UIColor lightGrayColor],
                                                                      NSFontAttributeName : self.messageLabel.font,
                                                                      NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType // no effect
                                                                      }] autorelease];

  [self.messageLabel setAttributedTruncationToken:atributedTruncationToken];

It looks perfect, but how can I make the token clickable?

(Particularly, I need the label to expand when user clicks on the token, but not on the rest of the label).

UPDATE. As I have found out, it is possible (iOS 7+) to add a link to the token, like the follows:

NSAttributedString *atributedTruncationToken = [[[NSAttributedString alloc]
                                                     initWithString:@" More..."
                                                         attributes:@{
                                                                      NSForegroundColorAttributeName : [UIColor lightGrayColor],
                                                                      NSFontAttributeName : self.messageLabel.font,
                                                                      NSLinkAttributeName : [NSURL URLWithString:@"..."]
                                                                      }] autorelease];

But there is kind of a bug (?) in TTTAttributed label, that the token still does not became clickable, but the n (n = token length) last characters of the label's text do!

1

There are 1 answers

1
hsusmita On

ResponsiveLabel, a subclass of UILabel can be used to configure clickable truncation token.

NSString *expansionToken = @"Read More ...";
NSString *str = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:kExpansionToken attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:self.customLabel.font}];
[self.customLabel setAttributedTruncationToken:attribString withAction:^(NSString *tappedString) {
  NSLog(@"Tap on truncation text");
}];
[self.customLabel setText:str withTruncation:YES];