Skip to content

Commit

Permalink
Fix ASTextNode2 is accessing backgroundColor off main while sizing / …
Browse files Browse the repository at this point in the history
…layout is happening (TextureGroup#794)
  • Loading branch information
maicki authored and bernieperez committed Apr 25, 2018
1 parent 65b61d1 commit c6c53a4
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
[self _ensureTruncationText];

NSMutableAttributedString *mutableText = [attributedText mutableCopy];
[self prepareAttributedStringForDrawing:mutableText];
[self prepareAttributedString:mutableText];
ASTextLayout *layout = [ASTextNode2 compatibleLayoutWithContainer:container text:mutableText];

[self setNeedsDisplay];
Expand Down Expand Up @@ -319,7 +319,7 @@ - (NSArray *)exclusionPaths
return _textContainer.exclusionPaths;
}

- (void)prepareAttributedStringForDrawing:(NSMutableAttributedString *)attributedString
- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString
{
ASDN::MutexLocker lock(__instanceLock__);

Expand All @@ -334,12 +334,6 @@ - (void)prepareAttributedStringForDrawing:(NSMutableAttributedString *)attribute
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
}];

// Apply background color if needed
UIColor *backgroundColor = self.backgroundColor;
if (CGColorGetAlpha(backgroundColor.CGColor) > 0) {
[attributedString addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:NSMakeRange(0, attributedString.length)];
}

// Apply shadow if needed
if (_shadowOpacity > 0 && (_shadowRadius != 0 || !CGSizeEqualToSize(_shadowOffset, CGSizeZero)) && CGColorGetAlpha(_shadowColor) > 0) {
NSShadow *shadow = [[NSShadow alloc] init];
Expand All @@ -362,11 +356,19 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
ASTextContainer *copiedContainer = [_textContainer copy];
copiedContainer.size = self.bounds.size;
NSMutableAttributedString *mutableText = [self.attributedText mutableCopy] ?: [[NSMutableAttributedString alloc] init];
[self prepareAttributedStringForDrawing:mutableText];

[self prepareAttributedString:mutableText];

// Apply background color if needed before drawing. To access the backgroundColor we need to be on the main thread
UIColor *backgroundColor = self.backgroundColor;
if (CGColorGetAlpha(backgroundColor.CGColor) > 0) {
[mutableText addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:NSMakeRange(0, mutableText.length)];
}

return @{
@"container": copiedContainer,
@"text": mutableText
};
@"container": copiedContainer,
@"text": mutableText
};
}

/**
Expand Down

0 comments on commit c6c53a4

Please sign in to comment.