属性突出显示标签在 UITableView 中无法正常工作?
AMAttributedHighlightLabel not working properly in UITable View?
我已经使用 AMAttributedHighlightLabel 显示“#"hashTag and "@”提及名称可点击,但 AMAttributedHighlightLabel 在合适的视图中正常工作。它将调用 touchesBegan 方法但未找到该词。这样它就不会触发委托方法。Table Label Image Link
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];
}
}
}
我在 AMAttributedHighlightLabel 库中遇到了一些问题,所以当在文本中使用表情符号字符时,我已经转移到 STTweetLabel 中的 STTweetLabel Library.This is good library but i have found 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;
}
我已经使用 AMAttributedHighlightLabel 显示“#"hashTag and "@”提及名称可点击,但 AMAttributedHighlightLabel 在合适的视图中正常工作。它将调用 touchesBegan 方法但未找到该词。这样它就不会触发委托方法。Table Label Image Link
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];
}
}
}
我在 AMAttributedHighlightLabel 库中遇到了一些问题,所以当在文本中使用表情符号字符时,我已经转移到 STTweetLabel 中的 STTweetLabel Library.This is good library but i have found 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;
}