【发布时间】:2015-05-24 17:36:01
【问题描述】:
我使用 AMAttributedHighlightLabel 显示“#”hashTag 和“@”提及名称可点击,但 AMAttributedHighlightLabel 在合适的视图中正常工作。它会调用 touchesBegan 方法但没有找到那个词。这样它就不会触发委托方法。Table Label Image Link
链接第三方自定义标签AMAttributedHighlightLabel
代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
int count = [touchableLocations count];
for (int i=0; i < count; i++)
{
if (CGRectContainsPoint([[touchableLocations objectAtIndex:i] CGRectValue], touchLocation))
{
NSMutableAttributedString *newAttrString = [self.attributedText mutableCopy];
[newAttrString removeAttribute:NSForegroundColorAttributeName range:[[touchableWordsRange objectAtIndex:i] rangeValue]];
NSString *string = [touchableWords objectAtIndex:i];
if([string hasPrefix:@"@"])
[newAttrString addAttribute:NSForegroundColorAttributeName value:self.selectedMentionTextColor range:[[touchableWordsRange objectAtIndex:i] rangeValue]];
else if ([string hasPrefix:@"#"])
[newAttrString addAttribute:NSForegroundColorAttributeName value:selectedHashtagTextColor range:[[touchableWordsRange objectAtIndex:i] rangeValue]];
else if ([string hasPrefix:@"http://"])
[newAttrString addAttribute:NSForegroundColorAttributeName value:selectedLinkTextColor range:[[touchableWordsRange objectAtIndex:i] rangeValue]];
else if ([string hasPrefix:@"https://"])
[newAttrString addAttribute:NSForegroundColorAttributeName value:selectedLinkTextColor range:[[touchableWordsRange objectAtIndex:i] rangeValue]];
else if ([string hasPrefix:@"www."])
[newAttrString addAttribute:NSForegroundColorAttributeName value:selectedLinkTextColor range:[[touchableWordsRange objectAtIndex:i] rangeValue]];
self.attributedText = newAttrString;
currentSelectedRange = [[touchableWordsRange objectAtIndex:i] rangeValue];
currentSelectedString = [touchableWords objectAtIndex:i];
}
}
}
【问题讨论】:
-
是否调用了任何代码?你说它通过了
touchesBegan:withEvent:,但它是否通过了if测试?价值观是什么? -
嗨@Larme它将检查“if”条件,但不匹配存储在数组touchableLocation中的corditionates。
-
当用户触摸哈希标签时,触摸方法被调用,我将得到那个点 (CGPoint) touchLocation = (x=145, y=39) 并在数组列表中显示该值,打印描述self->touchableLocations: <__nsarraym>( NSRect: {{413, 1.8450000286102295}, {8, 17}}, NSRect: {{421, 1.8450000286102295}, {9, 17}}, NSRect: {0}029, 1.824506 , {8, 17}}, NSRect: {{437, 1.8450000286102295}, {8, 17}}, NSRect: {{444, 1.8450000286102295}, {8, 17}}, NSRect: {{452, 1.8450000286102295}, { 7, 17}}, NSRect: {{459, 1.8450000286102295}, {8, 17}})
-
其实我已经用 UITableView 尝试过相同的库。我收到了所有事件。你能告诉我你的 UILabel 的层次结构是什么吗?像 UITableVIew --> UITableViewCell --> UILabel。这是我用来测试 senario 的层次结构,它工作正常。
-
您好@Jay Mehta,我已经尝试过相同的层次结构并且工作正常。我建议您一件事,最初您将 NSRect 存储在数组中与您在点击同一链接时收到的位置之间应该存在差异。所以我认为这种差异是由于它的层次结构。如果我能帮助你,请告诉我。
标签: ios iphone uitableview label uilabel