diff --git a/AsyncDisplayKit/TextKit/ASTextKitRenderer+Positioning.mm b/AsyncDisplayKit/TextKit/ASTextKitRenderer+Positioning.mm index 10cb53e7d5..57ae440fa1 100755 --- a/AsyncDisplayKit/TextKit/ASTextKitRenderer+Positioning.mm +++ b/AsyncDisplayKit/TextKit/ASTextKitRenderer+Positioning.mm @@ -27,9 +27,8 @@ static const CGFloat ASTextKitRendererTextCapHeightPadding = 1.3; { __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; } @@ -41,7 +40,7 @@ static const CGFloat ASTextKitRendererTextCapHeightPadding = 1.3; 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, diff --git a/AsyncDisplayKitTests/ASTextKitTests.mm b/AsyncDisplayKitTests/ASTextKitTests.mm index 90ef7443a1..426116d156 100644 --- a/AsyncDisplayKitTests/ASTextKitTests.mm +++ b/AsyncDisplayKitTests/ASTextKitTests.mm @@ -11,8 +11,10 @@ #import +#import "ASTextKitEntityAttribute.h" #import "ASTextKitAttributes.h" #import "ASTextKitRenderer.h" +#import "ASTextKitRenderer+Positioning.h" @interface ASTextKitTests : XCTestCase @@ -133,4 +135,20 @@ static BOOL checkAttributes(const ASTextKitAttributes &attributes, const CGSize 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