Do not reuse NSStringDrawingContexts (#2537)

This commit is contained in:
Garrett Moon 2016-11-02 14:37:51 -07:00 committed by Adlai Holler
parent ff9e493a59
commit 076d22e9f1

View File

@ -35,20 +35,11 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
return truncationCharacterSet;
}
@interface ASTextKitRenderer()
/**
* This object is lazily created. It is provided to the NSAttributedString
* drawing methods used by the fast-paths in the size calculation and drawing
* instance methods.
*/
@property (nonatomic, strong, readonly) NSStringDrawingContext *stringDrawingContext;
@end
@implementation ASTextKitRenderer {
CGSize _calculatedSize;
BOOL _sizeIsCalculated;
}
@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster, stringDrawingContext = _stringDrawingContext;
@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster;
#pragma mark - Initialization
@ -118,15 +109,18 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
- (NSStringDrawingContext *)stringDrawingContext
{
if (_stringDrawingContext == nil) {
_stringDrawingContext = [[NSStringDrawingContext alloc] init];
if (isinf(_constrainedSize.width) == NO && _attributes.maximumNumberOfLines > 0) {
ASDisplayNodeAssert(_attributes.maximumNumberOfLines != 1, @"Max line count 1 is not supported in fast-path.");
[_stringDrawingContext setValue:@(_attributes.maximumNumberOfLines) forKey:@"maximumNumberOfLines"];
}
// String drawing contexts are not safe to use from more than one thread.
// i.e. if they are created on one thread, it is unsafe to use them on another.
// Therefore we always need to create a new one.
//
// http://web.archive.org/web/20140703122636/https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html
NSStringDrawingContext *stringDrawingContext = [[NSStringDrawingContext alloc] init];
if (isinf(_constrainedSize.width) == NO && _attributes.maximumNumberOfLines > 0) {
ASDisplayNodeAssert(_attributes.maximumNumberOfLines != 1, @"Max line count 1 is not supported in fast-path.");
[stringDrawingContext setValue:@(_attributes.maximumNumberOfLines) forKey:@"maximumNumberOfLines"];
}
return _stringDrawingContext;
return stringDrawingContext;
}
#pragma mark - Sizing