在聊天应用程序上工作。在我的自定义单元格中使用 TTTAttributedLabel。这样我就可以检测链接。我在我的自定义单元中实现了该代码。
self.tttAttributedLabel=[[TTTAttributedLabel alloc] init];
self.tttAttributedLabel.font = [UIFont systemFontOfSize:14.0];
self.tttAttributedLabel.numberOfLines = 0;
self.tttAttributedLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.tttAttributedLabel.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:(__bridge NSString *)kCTUnderlineStyleAttributeName];
self.tttAttributedLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
self.tttAttributedLabel.delegate = self;
[self.contentView addSubview:self.tttAttributedLabel];
我在下面的功能中崩溃了。我不知道为什么会有崩溃。似乎 self.links 不是一个有效的数组。
- (NSTextCheckingResult *)linkAtCharacterIndex:(CFIndex)idx
{
NSEnumerator *enumerator = [self.links reverseObjectEnumerator]; //crash on this
NSTextCheckingResult *result = nil;
while ((result = [enumerator nextObject])) {
if (NSLocationInRange((NSUInteger)idx, result.range)) {
return result;
}
}
return nil;
}