Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions AsyncDisplayKit/TextKit/ASTextKitRenderer+Positioning.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ - (NSArray *)rectsForTextRange:(NSRange)textRange
{
__block NSArray *textRects = @[];
[self.context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
BOOL textRangeIsValid = (NSMaxRange(textRange) <= [textStorage length]);
ASDisplayNodeCAssertTrue(textRangeIsValid);
if (!textRangeIsValid) {
NSRange clampedRange = NSIntersectionRange(textRange, NSMakeRange(0, [textStorage length]));
if (clampedRange.location == NSNotFound || clampedRange.length == 0) {
return;
}

Expand All @@ -41,7 +40,7 @@ - (NSArray *)rectsForTextRange:(NSRange)textRange

NSString *string = textStorage.string;

NSRange totalGlyphRange = [layoutManager glyphRangeForCharacterRange:textRange actualCharacterRange:NULL];
NSRange totalGlyphRange = [layoutManager glyphRangeForCharacterRange:clampedRange actualCharacterRange:NULL];

[layoutManager enumerateLineFragmentsForGlyphRange:totalGlyphRange usingBlock:^(CGRect rect,
CGRect usedRect,
Expand Down
12 changes: 6 additions & 6 deletions AsyncDisplayKit/TextKit/ASTextKitRenderer+TextChecking.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ - (NSTextCheckingResult *)textCheckingResultAtPoint:(CGPoint)point

if (truncationTokenRange.location == NSNotFound) {
// 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);


__block CGFloat minDistance = CGFLOAT_MAX;
[self enumerateTextIndexesAtPosition:point usingBlock:^(NSUInteger index, CGRect glyphBoundingRect, BOOL *stop){
if (index >= truncationTokenRange.location) {
result = [[ASTextKitTextCheckingResult alloc] initWithType:ASTextKitTextCheckingTypeTruncation
Expand All @@ -86,15 +87,14 @@ - (NSTextCheckingResult *)textCheckingResultAtPoint:(CGPoint)point
NSRange range;
NSDictionary *attributes = [attributedString attributesAtIndex:index effectiveRange:&range];
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
entityAttribute:entityAttribute
range:range];
minDistance = distance;
}
}
if (result != nil) {
*stop = YES;
}
}];
return result;
}
Expand Down
18 changes: 18 additions & 0 deletions AsyncDisplayKitTests/ASTextKitTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

#import <FBSnapshotTestCase/FBSnapshotTestController.h>

#import "ASTextKitEntityAttribute.h"
#import "ASTextKitAttributes.h"
#import "ASTextKitRenderer.h"
#import "ASTextKitRenderer+Positioning.h"

@interface ASTextKitTests : XCTestCase

Expand Down Expand Up @@ -133,4 +135,20 @@ - (void)testStringsWithVariableAttributes
XCTAssert(checkAttributes(attributes, { 100, 100 }));
}

- (void)testRectsForRangeBeyondTruncationSizeReturnsNonZeroNumberOfRects
{
NSAttributedString *attributedString =
[[NSAttributedString alloc]
initWithString:@"90's cray photo booth tote bag bespoke Carles. Plaid wayfarers Odd Future master cleanse tattooed four dollar toast small batch kale chips leggings meh photo booth occupy irony. " attributes:@{ASTextKitEntityAttributeName : [[ASTextKitEntityAttribute alloc] initWithEntity:@"entity"]}];
ASTextKitRenderer *renderer =
[[ASTextKitRenderer alloc]
initWithTextKitAttributes:{
.attributedString = attributedString,
.maximumNumberOfLines = 1,
.truncationAttributedString = [[NSAttributedString alloc] initWithString:@"... Continue Reading"]
}
constrainedSize:{ 100, 100 }];
XCTAssert([renderer rectsForTextRange:NSMakeRange(0, attributedString.length) measureOption:ASTextKitRendererMeasureOptionBlock].count > 0);
}

@end