归因于Highlight Label在UITableView中无法正常工作?

时间:2015-03-21 09:46:20

标签: ios iphone uitableview label uilabel

我使用AMAttributedHighlightLabel来显示"#" hashTag和" @"提及名称可点击但AMAttributedHighlightLabel在uitable视图中正常工作。它将调用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];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

#import <CoreText/CoreText.h>

   - (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;

        }