TTTAttributedLabel: Bug when adding link to truncation attributed string

The link attributed isn’t added to the attributed truncation token correctly.

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *text = @"The NSString class declares the programmatic interface for an object that manages immutable strings. An immutable string is a text string that is defined when it is created and subsequently cannot be changed. NSString is implemented to represent an array of Unicode characters, in other words, a text string.";
    for (int i = 0; i < 10; i++) {
        TTTAttributedLabel *l1 = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(10, 10*(i+1) + (i * 50) + 10, self.view.bounds.size.width - (i +1)*20, 50)];
        l1.delegate = self;
        l1.userInteractionEnabled = YES;
        l1.numberOfLines = 2;
        l1.attributedTruncationToken = [[NSAttributedString alloc] initWithString:@"...more" attributes:@{NSForegroundColorAttributeName : [UIColor blueColor],
                                                                                                          NSLinkAttributeName : [NSURL URLWithString:@"http://more.com"]}];
//        l1.backgroundColor = [UIColor redColor];
        l1.text = text;
        [self.view addSubview:l1];
        l1.layer.borderColor = [UIColor redColor].CGColor;
        l1.layer.borderWidth = 2;
    }

}
- (void)attributedLabel:(TTTAttributedLabel *)label
   didSelectLinkWithURL:(NSURL *)url {
    NSLog(@"Here");
}

Notice that the link underlines are also not placed in the correct locations

screen shot 2015-04-13 at 3 20 40 pm

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 23 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I fix it, add on line code at line 870, I will make a pull request later, maybe I need to change the unit test code later.

truncationString = [truncationString attributedSubstringFromRange:NSMakeRange(0, truncationString.length - attributedTruncationString.length)];//ADD THIS LINE
[truncationString appendAttributedString:attributedTruncationString];
CTLineRef truncationLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)truncationString);

@shaikshabana I known, you must reset TTTAttributedLabel, eg.

_labelContent.text = nil;  
_labelContent.linkAttributes = nil;  
_labelContent.attributedText = nil;  
_labelContent.attributedTruncationToken = nil;  

remove old links, special “text = nil”

@jhersh I know how to reproduce the bug, Just tap the ...More, the blue text will appear

I still get this issue with attributedTruncationToken,can anybody help me to resolve this simulator screen shot dec 12 2016 12 02 09 pm