Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ASTextNode2 is accessing backgroundColor off main while sizing / layout is happening #794

Merged
merged 1 commit into from
Feb 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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