|
35 | 35 | return truncationCharacterSet; |
36 | 36 | } |
37 | 37 |
|
38 | | -@interface ASTextKitRenderer() |
39 | | -/** |
40 | | - * This object is lazily created. It is provided to the NSAttributedString |
41 | | - * drawing methods used by the fast-paths in the size calculation and drawing |
42 | | - * instance methods. |
43 | | - */ |
44 | | -@property (nonatomic, strong, readonly) NSStringDrawingContext *stringDrawingContext; |
45 | | -@end |
46 | | - |
47 | 38 | @implementation ASTextKitRenderer { |
48 | 39 | CGSize _calculatedSize; |
49 | 40 | BOOL _sizeIsCalculated; |
50 | 41 | } |
51 | | -@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster, stringDrawingContext = _stringDrawingContext; |
| 42 | +@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster; |
52 | 43 |
|
53 | 44 | #pragma mark - Initialization |
54 | 45 |
|
@@ -118,15 +109,18 @@ - (ASTextKitContext *)context |
118 | 109 |
|
119 | 110 | - (NSStringDrawingContext *)stringDrawingContext |
120 | 111 | { |
121 | | - if (_stringDrawingContext == nil) { |
122 | | - _stringDrawingContext = [[NSStringDrawingContext alloc] init]; |
123 | | - |
124 | | - if (isinf(_constrainedSize.width) == NO && _attributes.maximumNumberOfLines > 0) { |
125 | | - ASDisplayNodeAssert(_attributes.maximumNumberOfLines != 1, @"Max line count 1 is not supported in fast-path."); |
126 | | - [_stringDrawingContext setValue:@(_attributes.maximumNumberOfLines) forKey:@"maximumNumberOfLines"]; |
127 | | - } |
| 112 | + // String drawing contexts are not safe to use from more than one thread. |
| 113 | + // i.e. if they are created on one thread, it is unsafe to use them on another. |
| 114 | + // Therefore we always need to create a new one. |
| 115 | + // |
| 116 | + // http://web.archive.org/web/20140703122636/https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html |
| 117 | + NSStringDrawingContext *stringDrawingContext = [[NSStringDrawingContext alloc] init]; |
| 118 | + |
| 119 | + if (isinf(_constrainedSize.width) == NO && _attributes.maximumNumberOfLines > 0) { |
| 120 | + ASDisplayNodeAssert(_attributes.maximumNumberOfLines != 1, @"Max line count 1 is not supported in fast-path."); |
| 121 | + [stringDrawingContext setValue:@(_attributes.maximumNumberOfLines) forKey:@"maximumNumberOfLines"]; |
128 | 122 | } |
129 | | - return _stringDrawingContext; |
| 123 | + return stringDrawingContext; |
130 | 124 | } |
131 | 125 |
|
132 | 126 | #pragma mark - Sizing |
|
0 commit comments