Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save knopp/a8d5f243801b71451530 to your computer and use it in GitHub Desktop.
Save knopp/a8d5f243801b71451530 to your computer and use it in GitHub Desktop.
From 4528dd8a4f8c93472c10dd27a875bd1d21ae0ec0 Mon Sep 17 00:00:00 2001
From: Matej Knopp <matej.knopp@gmail.com>
Date: Tue, 22 Dec 2015 22:09:00 +0100
Subject: [PATCH] initialize text renderer in background thread
Signed-off-by: Matej Knopp <matej.knopp@gmail.com>
---
AsyncDisplayKit/ASTextNode.mm | 78 +++++++++++++++++++++++++++++--------------
1 file changed, 53 insertions(+), 25 deletions(-)
diff --git a/AsyncDisplayKit/ASTextNode.mm b/AsyncDisplayKit/ASTextNode.mm
index f5d3b03..9f4e633 100644
--- a/AsyncDisplayKit/ASTextNode.mm
+++ b/AsyncDisplayKit/ASTextNode.mm
@@ -30,11 +30,21 @@ static const CGFloat ASTextNodeHighlightLightOpacity = 0.11;
static const CGFloat ASTextNodeHighlightDarkOpacity = 0.22;
static NSString *ASTextNodeTruncationTokenAttributeName = @"ASTextNodeTruncationAttribute";
+@interface ASTextNode () <UIGestureRecognizerDelegate>
+
+- (ASTextKitRenderer *)_rendererWithBounds:(CGRect)_bounds;
+
+@end
+
@interface ASTextNodeDrawParameters : NSObject
+{
+ ASTextNode *_node;
+ CGRect _bounds;
+ ASTextKitRenderer *_renderer;
+ CGPoint _textOrigin;
+}
-- (instancetype)initWithRenderer:(ASTextKitRenderer *)renderer
- textOrigin:(CGPoint)textOrigin
- backgroundColor:(CGColorRef)backgroundColor;
+- (instancetype)initWithNode:(ASTextNode*)node;
@property (nonatomic, strong, readonly) ASTextKitRenderer *renderer;
@@ -46,14 +56,13 @@ static NSString *ASTextNodeTruncationTokenAttributeName = @"ASTextNodeTruncation
@implementation ASTextNodeDrawParameters
-- (instancetype)initWithRenderer:(ASTextKitRenderer *)renderer
- textOrigin:(CGPoint)textOrigin
- backgroundColor:(CGColorRef)backgroundColor
+-(instancetype)initWithNode:(ASTextNode *)node
{
- if (self = [super init]) {
- _renderer = renderer;
- _textOrigin = textOrigin;
- _backgroundColor = CGColorRetain(backgroundColor);
+ if (self = [super init])
+ {
+ _node = node;
+ _bounds = node.bounds;
+ _backgroundColor = CGColorRetain(node.backgroundColor.CGColor);
}
return self;
}
@@ -63,9 +72,28 @@ static NSString *ASTextNodeTruncationTokenAttributeName = @"ASTextNodeTruncation
CGColorRelease(_backgroundColor);
}
-@end
-@interface ASTextNode () <UIGestureRecognizerDelegate>
+-(void)ensureInitialized
+{
+ if (!_renderer)
+ {
+ _renderer = [_node _rendererWithBounds:_bounds];
+ UIEdgeInsets shadowPadding = _renderer.shadower.shadowPadding;
+ _textOrigin = CGPointMake(_bounds.origin.x - shadowPadding.left, _bounds.origin.y - shadowPadding.top);
+ }
+}
+
+-(ASTextKitRenderer *)renderer
+{
+ [self ensureInitialized];
+ return _renderer;
+}
+
+-(CGPoint)textOrigin
+{
+ [self ensureInitialized];
+ return _textOrigin;
+}
@end
@@ -248,13 +276,18 @@ static NSString *ASTextNodeTruncationTokenAttributeName = @"ASTextNodeTruncation
- (ASTextKitRenderer *)_renderer
{
- ASDN::MutexLocker l(_rendererLock);
- if (_renderer == nil) {
- CGSize constrainedSize = _constrainedSize.width != -INFINITY ? _constrainedSize : self.bounds.size;
- _renderer = [[ASTextKitRenderer alloc] initWithTextKitAttributes:[self _rendererAttributes]
- constrainedSize:constrainedSize];
- }
- return _renderer;
+ return [self _rendererWithBounds:self.bounds];
+}
+
+- (ASTextKitRenderer *)_rendererWithBounds:(CGRect)_bounds
+{
+ ASDN::MutexLocker l(_rendererLock);
+ if (_renderer == nil) {
+ CGSize constrainedSize = _constrainedSize.width != -INFINITY ? _constrainedSize : _bounds.size;
+ _renderer = [[ASTextKitRenderer alloc] initWithTextKitAttributes:[self _rendererAttributes]
+ constrainedSize:constrainedSize];
+ }
+ return _renderer;
}
- (ASTextKitAttributes)_rendererAttributes
@@ -377,12 +410,7 @@ static NSString *ASTextNodeTruncationTokenAttributeName = @"ASTextNodeTruncation
- (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
{
- // Offset the text origin by any shadow padding
- UIEdgeInsets shadowPadding = [self shadowPadding];
- CGPoint textOrigin = CGPointMake(self.bounds.origin.x - shadowPadding.left, self.bounds.origin.y - shadowPadding.top);
- return [[ASTextNodeDrawParameters alloc] initWithRenderer:[self _renderer]
- textOrigin:textOrigin
- backgroundColor:self.backgroundColor.CGColor];
+ return [[ASTextNodeDrawParameters alloc] initWithNode:self];
}
#pragma mark - Attributes
--
2.5.4 (Apple Git-61)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment