[text] Fix crash when highlighted range exceeds truncation range

This commit is contained in:
yury 2016-01-26 22:37:44 +03:00
parent 1514d23d33
commit a0b7254213
2 changed files with 21 additions and 4 deletions

View File

@ -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,

View File

@ -11,8 +11,10 @@
#import <FBSnapshotTestCase/FBSnapshotTestController.h>
#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