Skip to content

Commit

Permalink
Do not reuse NSStringDrawingContexts (facebookarchive#2537)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmoon authored and Adlai Holler committed Nov 2, 2016
1 parent ff9e493 commit 076d22e
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions AsyncDisplayKit/TextKit/ASTextKitRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,11 @@
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

Expand Down Expand Up @@ -118,15 +109,18 @@ - (ASTextKitContext *)context

- (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
Expand Down

0 comments on commit 076d22e

Please sign in to comment.