[text] Use the closest glyph to determine hit token

This commit is contained in:
yury
2016-01-26 22:41:02 +03:00
parent a0b7254213
commit a3a0440a01

View File

@@ -72,11 +72,12 @@
if (truncationTokenRange.location == NSNotFound) { if (truncationTokenRange.location == NSNotFound) {
// The truncation string didn't specify a substring which should be highlighted, so we just highlight it all // The truncation string didn't specify a substring which should be highlighted, so we just highlight it all
truncationTokenRange = { 0, self.attributes.truncationAttributedString.length }; truncationTokenRange = { 0, truncationAttributedString.length };
} }
truncationTokenRange.location += NSMaxRange(visibleRange); truncationTokenRange.location += NSMaxRange(visibleRange);
__block CGFloat minDistance = CGFLOAT_MAX;
[self enumerateTextIndexesAtPosition:point usingBlock:^(NSUInteger index, CGRect glyphBoundingRect, BOOL *stop){ [self enumerateTextIndexesAtPosition:point usingBlock:^(NSUInteger index, CGRect glyphBoundingRect, BOOL *stop){
if (index >= truncationTokenRange.location) { if (index >= truncationTokenRange.location) {
result = [[ASTextKitTextCheckingResult alloc] initWithType:ASTextKitTextCheckingTypeTruncation result = [[ASTextKitTextCheckingResult alloc] initWithType:ASTextKitTextCheckingTypeTruncation
@@ -86,15 +87,14 @@
NSRange range; NSRange range;
NSDictionary *attributes = [attributedString attributesAtIndex:index effectiveRange:&range]; NSDictionary *attributes = [attributedString attributesAtIndex:index effectiveRange:&range];
ASTextKitEntityAttribute *entityAttribute = attributes[ASTextKitEntityAttributeName]; ASTextKitEntityAttribute *entityAttribute = attributes[ASTextKitEntityAttributeName];
if (entityAttribute) { CGFloat distance = hypot(CGRectGetMidX(glyphBoundingRect) - point.x, CGRectGetMidY(glyphBoundingRect) - point.y);
if (entityAttribute && distance < minDistance) {
result = [[ASTextKitTextCheckingResult alloc] initWithType:ASTextKitTextCheckingTypeEntity result = [[ASTextKitTextCheckingResult alloc] initWithType:ASTextKitTextCheckingTypeEntity
entityAttribute:entityAttribute entityAttribute:entityAttribute
range:range]; range:range];
minDistance = distance;
} }
} }
if (result != nil) {
*stop = YES;
}
}]; }];
return result; return result;
} }