【问题标题】:TTTAttributedLabel Delegate didSelectLinkWithURL not getting calledTTTAttributedLabel 委托 didSelectLinkWithURL 没有被调用
【发布时间】:2015-12-28 12:23:33
【问题描述】:

我在我的自定义单元格中设置TTTAttributedLabel 时遇到问题。我已经检查了很多帖子,但仍然没有想出如何解决这个问题。 (我可以在这个自定义标签上看到 URL 样式,但如果我点击它什么都不会发生)。

CustomCell.h:

@interface MyCustomCell : UITableViewCell<TTTAttributedLabelDelegate>

@property (nonatomic, strong, readonly) UITapGestureRecognizer *tapRecognizer;

@property (nonatomic, strong, readonly) UILabel *senderLabel;

@property (nonatomic, strong, readonly) UILabel *timeLabel;

@property (nonatomic, strong, readonly) UILabel *dateLabel;

@property (nonatomic, strong) TTTAttributedLabel *customTextLabel;

@end

CustomCell.m:

@implementation MyCustomCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {        
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _dateLabel.textAlignment = NSTextAlignmentCenter;
        _dateLabel.font = [UIFont boldSystemFontOfSize:12.0];
        _dateLabel.textColor = [UIColor grayColor];
        _dateLabel.text = @"2015-10-10";
        _dateLabel.userInteractionEnabled = false;

        _senderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _senderLabel.textAlignment = NSTextAlignmentLeft;
        _senderLabel.font = [UIFont boldSystemFontOfSize:14.0];
        _senderLabel.textColor = [UIColor whiteColor];
        _senderLabel.userInteractionEnabled = false;

        _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _timeLabel.userInteractionEnabled = false;
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.font = [UIFont boldSystemFontOfSize:11.0];
        _timeLabel.textColor = [UIColor whiteColor];

        _customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        _customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
        _customTextLabel.delegate = self;

        _customTextLabel.backgroundColor = [UIColor clearColor];
        _customTextLabel.numberOfLines = 0;
        _customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _customTextLabel.textColor = [UIColor blackColor];
        _customTextLabel.font = [UIFont systemFontOfSize:18.0];

        [self.textLabel addSubview:_customTextLabel];
        _customTextLabel.userInteractionEnabled = true;

        self.imageView.userInteractionEnabled = YES;
        self.imageView.layer.cornerRadius = 5.0;
        self.imageView.layer.masksToBounds = YES;
    }

    return self;
}

感谢您的帮助。

【问题讨论】:

  • 如果你只想要触摸事件,那么你应该在 uilabel 上使用 UITapGestureRecognizer
  • 您尚未发布应在 TTTAttributedLabel 单击时调用的委托方法。您如何创建单元格还不清楚。
  • 您是否为 _customTextLabel 分配了任何文本?
  • 你在 self.textLabel 中添加 _customTextLabel: [self.textLabel addSubview:_customTextLabel]; textLabel 是 UILabel 吗?
  • - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { NSLog(@"Do Something"); }

标签: ios objective-c iphone


【解决方案1】:
    NSString *text = @"Hello this is https://www.google.com";    
    _customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 100, 300, 20)];
    _customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    _customTextLabel.delegate = self;

    _customTextLabel.backgroundColor = [UIColor clearColor];
    _customTextLabel.numberOfLines = 0;
    _customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _customTextLabel.textColor = [UIColor blackColor];
    _customTextLabel.font = [UIFont systemFontOfSize:18.0];
    _customTextLabel.text = text;
    [self.view addSubview:_customTextLabel];

并设置TTTAttributedLabel的委托方法,它工作正常请检查:

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
}

【讨论】:

  • 感谢您的回答,但这不是常规视图,因为您可以看到它是 tableViewCell,我尝试按照您的建议添加 [self.textLabel addSubview:_customTextLabel];但它仍然不起作用。
  • self.textLabel 是 UILabel 吗?
  • 你能看到文本中的url吗?
  • self.textLabel 是默认单元格标签,是的,我可以看到 URL 样式。
  • 您是否尝试过在单元格中添加 _customTextLabel 而不是 textLabel?
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
相关资源
最近更新 更多