Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

[ASTextNode] Fix wrong truncation after constrained size changes #1924

Merged
merged 2 commits into from
Jul 15, 2016
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: 5 additions & 2 deletions AsyncDisplayKit/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,10 @@ - (void)calculatedLayoutDidChange

if (layout != nil) {
ASDN::MutexLocker l(_propertyLock);
_constrainedSize = layout.size;
_renderer.constrainedSize = layout.size;
if (CGSizeEqualToSize(_constrainedSize, layout.size) == NO) {
_constrainedSize = layout.size;
_renderer.constrainedSize = layout.size;
}
}
}

Expand Down Expand Up @@ -397,6 +399,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
// Tell the display node superclasses that the cached layout is incorrect now
[self invalidateCalculatedLayout];

// Force display to create renderer with new size and redisplay with new string
[self setNeedsDisplay];


Expand Down
16 changes: 8 additions & 8 deletions AsyncDisplayKit/TextKit/ASTextKitRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ - (void)setConstrainedSize:(CGSize)constrainedSize
if (!CGSizeEqualToSize(constrainedSize, _constrainedSize)) {
_sizeIsCalculated = NO;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also reset _calculatedSize here, to CGSizeZero I guess.

_constrainedSize = constrainedSize;
// If the context isn't created yet, it will be initialized with the appropriate size when next accessed.
if (_context || _fontSizeAdjuster) {
// If we're updating an existing context, make sure to use the same inset logic used during initialization.
// This codepath allows us to reuse the
CGSize shadowConstrainedSize = [[self shadower] insetSizeWithConstrainedSize:constrainedSize];
if (_context) _context.constrainedSize = shadowConstrainedSize;
if (_fontSizeAdjuster) _fontSizeAdjuster.constrainedSize = shadowConstrainedSize;
}

// Throw away the all subcomponents to create them with the new constrained size new as well as let the
// truncater do it's job again for the new constrained size. This is necessary as after a truncation did happen
// the context would use the truncated string and not the original string to truncate based on the new
// constrained size
_context = nil;
_truncater = nil;
_fontSizeAdjuster = nil;
}
}

Expand Down
4 changes: 2 additions & 2 deletions AsyncDisplayKitTests/ASTextNodeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ - (void)testRecalculationOfSizeIsSameAsOriginallyCalculatedSize
for (NSInteger i = 10; i < 500; i += 50) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode measure:constrainedSize];
CGSize recalculatedSize = [_textNode measure:calculatedSize];
CGSize recalculatedSize = [_textNode measure:constrainedSize];

XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 4.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}
Expand All @@ -136,7 +136,7 @@ - (void)testRecalculationOfSizeIsSameAsOriginallyCalculatedFloatingPointSize
for (CGFloat i = 10; i < 500; i *= 1.3) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode measure:constrainedSize];
CGSize recalculatedSize = [_textNode measure:calculatedSize];
CGSize recalculatedSize = [_textNode measure:constrainedSize];

XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 11.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}
Expand Down