TTTAttributedLabel可点击截断令牌

时间:2015-06-16 03:40:18

标签: ios uilabel tttattributedlabel

我有一个TTTAttributedLabel并为它指定了一个自定义的截断标记:

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

  [self.messageLabel setAttributedTruncationToken:atributedTruncationToken];

它看起来很完美,但如何才能使令牌可点击?

(特别是,我需要在用户点击令牌时展开标签,但不要在标签的其余部分展开。

更新 正如我所知,有可能(iOS 7+)添加一个指向令牌的链接,如下所示:

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

但TTTAttributed标签中存在一种错误(?),该令牌仍然无法点击,但标签文本的最后一个字符是n(n = token length)!

1 个答案:

答案 0 :(得分:0)

ResponsiveLabel,UILabel的子类可用于配置可点击的截断标记。

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];
相关问题