Ios TTtatAttributedLabel委托未选择URL未被调用的链接
在我的自定义单元格中设置Ios TTtatAttributedLabel委托未选择URL未被调用的链接,ios,objective-c,iphone,Ios,Objective C,Iphone,在我的自定义单元格中设置TTTAttributedLabel时遇到问题。我已经检查了很多帖子,仍然没有找到解决方法。(我可以在这个自定义标签上看到URL样式,但是如果我单击它,什么也不会发生) CustomCell.h: @interface MyCustomCell : UITableViewCell<TTTAttributedLabelDelegate> @property (nonatomic, strong, readonly) UITapGestureRecognizer
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
@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;
}
谢谢你的帮助
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 {
}
并设置TTTAttributedLabel
的委托方法,工作正常,请检查:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
}
如果您只需要触摸事件,则必须在UILabel上使用UIAPTestureRecognitizer。如果您尚未发布委托方法,则应在TTtatAttributedLabel单击时调用该方法。您是如何创建单元格的还不清楚。您是否为_customTextLabel指定了任何文本?您正在self.textlab:[self.textlab addSubview:_customTextLabel]中添加_customTextLabel;textLabel是UILabel吗?-(void)attributedLabel:(TTtatAttributedLabel*)label didSelectLinkWithURL:(NSURL*)url{NSLog(@“Do Something”);}如果您只想要触摸事件,则必须在UILabel上使用UIAPTestureRecogener。如果您没有发布应在TTtatAttributedLabel单击时调用的委托方法。您是如何创建单元格的还不清楚。您是否为_customTextLabel指定了任何文本?您正在self.textlab:[self.textlab addSubview:_customTextLabel]中添加_customTextLabel;textLabel是UILabel吗?-(void)attributedLabel:(TTTAttributedLabel*)label didSelectLinkWithURL:(NSURL*)url{NSLog(@“Do Something”);}谢谢您的回答,但它不是常规视图,因为您可以看到它的tableViewCell,我尝试按照您的建议添加了[self.textLabel addSubview:_customTextLabel];但是它仍然不起作用。self.textLabel是UILabel?你能在文本中看到url吗?self.textLabel是默认的单元格标签,是的,我可以看到url样式。你有没有尝试在单元格中添加_customTextLabel而不是textLabel?谢谢你的回答,但它不是常规视图,因为你可以看到它是tableViewCell,我尝试按照您的建议添加[self.textLabel addSubview:_customTextLabel];但是它仍然不起作用。self.textLabel是UILabel?你能在文本中看到url吗?self.textLabel是默认的单元格标签,是的,我可以看到url样式。你试过在单元格中添加_customTextLabel而不是textLabel吗?