【发布时间】: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