TTTAttributedLabel clickable truncation token

£可爱£侵袭症+ 提交于 2019-12-05 04:29:19

问题


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:


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


来源:https://stackoverflow.com/questions/30858370/tttattributedlabel-clickable-truncation-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!