diff --git a/AsyncDisplayKit/ASTextNode.mm b/AsyncDisplayKit/ASTextNode.mm index e0be8e330e..bf7ca79005 100644 --- a/AsyncDisplayKit/ASTextNode.mm +++ b/AsyncDisplayKit/ASTextNode.mm @@ -354,6 +354,7 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ]; if (layout != nil) { ASDN::MutexLocker l(__instanceLock__); CGSize layoutSize = layout.size; + //Apply textContainerInset layoutSize.width -= (_textContainerInset.left + _textContainerInset.right); layoutSize.height -= (_textContainerInset.top + _textContainerInset.bottom); @@ -367,12 +368,15 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ]; - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize { + ASDN::MutexLocker l(__instanceLock__); + ASDisplayNodeAssert(constrainedSize.width >= 0, @"Constrained width for text (%f) is too narrow", constrainedSize.width); ASDisplayNodeAssert(constrainedSize.height >= 0, @"Constrained height for text (%f) is too short", constrainedSize.height); - ASDN::MutexLocker l(__instanceLock__); + // Cache the original constrained size for final size calculateion + CGSize originalConstrainedSize = constrainedSize; - //remove textContainerInset + // Adjust constrainedSize for textContainerInset before assigning it constrainedSize.width -= (_textContainerInset.left + _textContainerInset.right); constrainedSize.height -= (_textContainerInset.top + _textContainerInset.bottom); @@ -395,11 +399,12 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ]; } } - //add textContainerInset + // Add the constrained size back textContainerInset size.width += (_textContainerInset.left + _textContainerInset.right); size.height += (_textContainerInset.top + _textContainerInset.bottom); - return size; + return CGSizeMake(std::fmin(size.width, originalConstrainedSize.width), + std::fmin(size.height, originalConstrainedSize.height)); } #pragma mark - Modifying User Text diff --git a/AsyncDisplayKitTests/ASTextNodeSnapshotTests.m b/AsyncDisplayKitTests/ASTextNodeSnapshotTests.m index 5fb5a3f556..b23795cf8a 100644 --- a/AsyncDisplayKitTests/ASTextNodeSnapshotTests.m +++ b/AsyncDisplayKitTests/ASTextNodeSnapshotTests.m @@ -31,6 +31,28 @@ ASSnapshotVerifyNode(textNode, nil); } +- (void)testTextContainerInsetIsIncludedWithSmallerConstrainedSize +{ + UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; + backgroundView.layer.as_allowsHighlightDrawing = YES; + + ASTextNode *textNode = [[ASTextNode alloc] init]; + textNode.attributedText = [[NSAttributedString alloc] initWithString:@"judar judar judar judar judar judar" + attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:30] }]; + + [textNode measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 80))]; + textNode.frame = CGRectMake(50, 50, textNode.calculatedSize.width, textNode.calculatedSize.height); + textNode.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10); + + [backgroundView addSubview:textNode.view]; + backgroundView.frame = UIEdgeInsetsInsetRect(textNode.bounds, UIEdgeInsetsMake(-50, -50, -50, -50)); + + textNode.highlightRange = NSMakeRange(0, textNode.attributedText.length); + + [ASSnapshotTestCase hackilySynchronouslyRecursivelyRenderNode:textNode]; + FBSnapshotVerifyLayer(backgroundView.layer, nil); +} + - (void)testTextContainerInsetHighlight { UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; diff --git a/AsyncDisplayKitTests/ReferenceImages_64/ASTextNodeSnapshotTests/testTextContainerInsetIsIncludedWithSmallerConstrainedSize@2x.png b/AsyncDisplayKitTests/ReferenceImages_64/ASTextNodeSnapshotTests/testTextContainerInsetIsIncludedWithSmallerConstrainedSize@2x.png new file mode 100644 index 0000000000..d5c6cdfcb3 Binary files /dev/null and b/AsyncDisplayKitTests/ReferenceImages_64/ASTextNodeSnapshotTests/testTextContainerInsetIsIncludedWithSmallerConstrainedSize@2x.png differ