3

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!

Andrey Solovyov
  • 551
  • 7
  • 21
  • i am also facing the same problem. Truncated text is not clickable instead the last few characters get clicked.Do you solve this problem,if yes please let me know. – Aditya Jun 08 '16 at 15:05
  • Nope, I did not even try. I just implemented my own clumsy label, using CoreText, UIButton and delegate. If it is possible, use newer version of TTTAttributedLabel. – Andrey Solovyov Jun 08 '16 at 15:31
  • 1
    in newer version also i am facing the same problem..sometime it is clickable if the truncated token is at the end table row.but if comes in between of row, then it is not working – Aditya Jun 08 '16 at 15:39

1 Answers1

0

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];
hsusmita
  • 282
  • 3
  • 16
  • I tried [ResponsiveLabel](https://github.com/hsusmita/ResponsiveLabel/), and in the demo project I have very weird scrolling issues on expanding the label in a table view. – pommefrite Apr 04 '16 at 10:43