Stopped trying to cache the currentScaleFactor

Was running into issues where the scale factor would get cleared when setting a new atributedString on a textNode.

I was clearing out the currentScaleFactor when setting an attributedString into a textNode. It appears that `_calculateSize` isn't always called when setting a new string into a ASTextNode. It can be the case that only `drawInContext:bounds:` is called. With _currentScaleFactor cleared out the renderer that calls`drawInContext...` was being called with a scaleFactor of 0.

It could be the case that the fix could be to remove the clearing of `currentScaleFactor` from `setAttributedString`, but this seems like a safer fix to me. It does, however, require an extra run through the font adjuster when enabled.
This commit is contained in:
rcancro
2016-02-23 16:51:10 -08:00
parent 92f87756b3
commit 878fedd8f3
4 changed files with 8 additions and 22 deletions

View File

@@ -15,9 +15,4 @@
*/ */
@property (nonatomic, copy) NSArray *pointSizeScaleFactors; @property (nonatomic, copy) NSArray *pointSizeScaleFactors;
/**
@abstract The currently applied scale factor, or 0 if the text node is not being scaled.
*/
@property (nonatomic, assign, readonly) CGFloat currentScaleFactor;
@end @end

View File

@@ -244,7 +244,6 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ];
.maximumNumberOfLines = _maximumNumberOfLines, .maximumNumberOfLines = _maximumNumberOfLines,
.exclusionPaths = _exclusionPaths, .exclusionPaths = _exclusionPaths,
.pointSizeScaleFactors = _pointSizeScaleFactors, .pointSizeScaleFactors = _pointSizeScaleFactors,
.currentScaleFactor = self.currentScaleFactor,
.layoutManagerCreationBlock = self.layoutManagerCreationBlock, .layoutManagerCreationBlock = self.layoutManagerCreationBlock,
.textStorageCreationBlock = self.textStorageCreationBlock, .textStorageCreationBlock = self.textStorageCreationBlock,
}; };
@@ -338,10 +337,7 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ];
[self setNeedsDisplay]; [self setNeedsDisplay];
CGSize size = [[self _renderer] size]; return [[self _renderer] size];
// the renderer computes the current scale factor during sizing, so let's grab it here
_currentScaleFactor = _renderer.currentScaleFactor;
return size;
} }
#pragma mark - Modifying User Text #pragma mark - Modifying User Text
@@ -380,8 +376,6 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ];
self.isAccessibilityElement = YES; self.isAccessibilityElement = YES;
} }
// reset the scale factor if we get a new string.
_currentScaleFactor = 0;
if (attributedString.length > 0) { if (attributedString.length > 0) {
CGFloat screenScale = ASScreenScale(); CGFloat screenScale = ASScreenScale();
self.ascender = round([[attributedString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL] ascender] * screenScale)/screenScale; self.ascender = round([[attributedString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL] ascender] * screenScale)/screenScale;

View File

@@ -86,10 +86,6 @@ struct ASTextKitAttributes {
An array of scale factors in descending order to apply to the text to try to make it fit into a constrained size. An array of scale factors in descending order to apply to the text to try to make it fit into a constrained size.
*/ */
NSArray *pointSizeScaleFactors; NSArray *pointSizeScaleFactors;
/**
The currently applied scale factor. Only valid if pointSizeScaleFactors are provided. Defaults to 0 (no scaling)
*/
CGFloat currentScaleFactor;
/** /**
An optional block that returns a custom layout manager subclass. If nil, defaults to NSLayoutManager. An optional block that returns a custom layout manager subclass. If nil, defaults to NSLayoutManager.
*/ */
@@ -123,7 +119,6 @@ struct ASTextKitAttributes {
shadowOpacity, shadowOpacity,
shadowRadius, shadowRadius,
pointSizeScaleFactors, pointSizeScaleFactors,
currentScaleFactor,
layoutManagerCreationBlock, layoutManagerCreationBlock,
layoutManagerDelegate, layoutManagerDelegate,
textStorageCreationBlock, textStorageCreationBlock,
@@ -138,7 +133,6 @@ struct ASTextKitAttributes {
&& shadowOpacity == other.shadowOpacity && shadowOpacity == other.shadowOpacity
&& shadowRadius == other.shadowRadius && shadowRadius == other.shadowRadius
&& [pointSizeScaleFactors isEqualToArray:other.pointSizeScaleFactors] && [pointSizeScaleFactors isEqualToArray:other.pointSizeScaleFactors]
&& currentScaleFactor == currentScaleFactor
&& layoutManagerCreationBlock == other.layoutManagerCreationBlock && layoutManagerCreationBlock == other.layoutManagerCreationBlock
&& textStorageCreationBlock == other.textStorageCreationBlock && textStorageCreationBlock == other.textStorageCreationBlock
&& CGSizeEqualToSize(shadowOffset, other.shadowOffset) && CGSizeEqualToSize(shadowOffset, other.shadowOffset)

View File

@@ -49,9 +49,6 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
_constrainedSize = constrainedSize; _constrainedSize = constrainedSize;
_attributes = attributes; _attributes = attributes;
_sizeIsCalculated = NO; _sizeIsCalculated = NO;
if ([attributes.pointSizeScaleFactors count] > 0) {
_currentScaleFactor = attributes.currentScaleFactor;
}
} }
return self; return self;
} }
@@ -141,7 +138,8 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
- (void)_calculateSize - (void)_calculateSize
{ {
[self truncater]; [self truncater];
if ([_attributes.pointSizeScaleFactors count] > 0) { // if we have no scale factors or an unconstrained width, there is no reason to try to adjust the font size
if ([_attributes.pointSizeScaleFactors count] > 0 && isinf(_constrainedSize.width) == NO) {
_currentScaleFactor = [[self fontSizeAdjuster] scaleFactor]; _currentScaleFactor = [[self fontSizeAdjuster] scaleFactor];
} }
@@ -175,6 +173,11 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
// We add an assertion so we can track the rare conditions where a graphics context is not present // We add an assertion so we can track the rare conditions where a graphics context is not present
ASDisplayNodeAssertNotNil(context, @"This is no good without a context."); ASDisplayNodeAssertNotNil(context, @"This is no good without a context.");
// This renderer may not be the one that did the sizing. If that is the case its _currentScaleFactor will not be set, so we should compute it now
if ([_attributes.pointSizeScaleFactors count] > 0 && isinf(_constrainedSize.width) == NO && _sizeIsCalculated == NO) {
_currentScaleFactor = [[self fontSizeAdjuster] scaleFactor];
}
CGRect shadowInsetBounds = [[self shadower] insetRectWithConstrainedRect:bounds]; CGRect shadowInsetBounds = [[self shadower] insetRectWithConstrainedRect:bounds];
CGContextSaveGState(context); CGContextSaveGState(context);