From 082ababda67d69d8c00947e7adcb9fb5ecf48d8e Mon Sep 17 00:00:00 2001 From: rcancro Date: Mon, 9 May 2016 15:57:29 -0700 Subject: [PATCH] Fix to text shrinking Summary: Previously the first thing we did when calculating the size of text was to truncate it. This could lead to ASFontSizeAdjuster being sent a truncated string which, obviously, never needed to shrink. If we change the order then we first shrink, then truncate if still necessary. Reviewers: scottg, levi, garrett Differential Revision: https://phabricator.pinadmin.com/D89780 --- AsyncDisplayKit/TextKit/ASTextKitRenderer.mm | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/AsyncDisplayKit/TextKit/ASTextKitRenderer.mm b/AsyncDisplayKit/TextKit/ASTextKitRenderer.mm index bc7d2c1069..035db7977f 100755 --- a/AsyncDisplayKit/TextKit/ASTextKitRenderer.mm +++ b/AsyncDisplayKit/TextKit/ASTextKitRenderer.mm @@ -137,25 +137,30 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet() - (void)_calculateSize { - [self truncater]; // if we have no scale factors or an unconstrained width, there is no reason to try to adjust the font size if (isinf(_constrainedSize.width) == NO && [_attributes.pointSizeScaleFactors count] > 0) { _currentScaleFactor = [[self fontSizeAdjuster] scaleFactor]; } - // Force glyph generation and layout, which may not have happened yet (and isn't triggered by - // -usedRectForTextContainer:). __block NSTextStorage *scaledTextStorage = nil; BOOL isScaled = [self isScaled]; - [[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) { - if (isScaled) { + if (isScaled) { + // apply the string scale before truncating or else we may truncate the string after we've done the work to shrink it. + [[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) { NSMutableAttributedString *scaledString = [[NSMutableAttributedString alloc] initWithAttributedString:textStorage]; [ASTextKitFontSizeAdjuster adjustFontSizeForAttributeString:scaledString withScaleFactor:_currentScaleFactor]; scaledTextStorage = [[NSTextStorage alloc] initWithAttributedString:scaledString]; [textStorage removeLayoutManager:layoutManager]; [scaledTextStorage addLayoutManager:layoutManager]; - } + }]; + } + + [self truncater]; + + // Force glyph generation and layout, which may not have happened yet (and isn't triggered by + // -usedRectForTextContainer:). + [[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) { [layoutManager ensureLayoutForTextContainer:textContainer]; }];