【问题标题】:AMAttributedHighlightLabel not working properly in UITable View?属性高亮标签在 UITableView 中无法正常工作?
【发布时间】: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


【解决方案1】:

我在 AMAttributedHighlightLabel 库中遇到了一些问题,因此我已移至 STTweetLabel 库。这是一个很好的库,但我在 STTweetLabel 中找到了 issue,当在文本中使用表情符号字符时,它会减少标签上的一些文本这样我将使用下面的代码

#import &lt;CoreText/CoreText.h&gt;

   - (CGFloat)heightStringWithEmojis:(NSString*)str fontType:(UIFont *)uiFont ForWidth:(CGFloat)width {

            // Get text
            CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
            CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef) str );
            CFIndex stringLength = CFStringGetLength((CFStringRef) attrString);

            // Change font
            CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
            CFAttributedStringSetAttribute(attrString, CFRangeMake(0, stringLength), kCTFontAttributeName, ctFont);

            // Calc the size
            CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
            CFRange fitRange;
            CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange);

            CFRelease(ctFont);
            CFRelease(framesetter);
            CFRelease(attrString);

            return frameSize.height + 5;

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多