diff --git a/ComponentTextKit/CKTextComponentLayer.mm b/ComponentTextKit/CKTextComponentLayer.mm index 31639fe85..79789cacb 100644 --- a/ComponentTextKit/CKTextComponentLayer.mm +++ b/ComponentTextKit/CKTextComponentLayer.mm @@ -85,15 +85,30 @@ - (NSObject *)drawParameters - (id)willDisplayAsynchronouslyWithDrawParameters:(id)drawParameters { - return rasterContentsCache()->objectForKey({_renderer.attributes, _renderer.constrainedSize}); + NSInteger userInterfaceStyle = 1; + if (@available(iOS 13.0, *)) { + UIView *view = (UIView *)self.delegate; + if ([view isKindOfClass:UIView.class]) { + userInterfaceStyle = (NSInteger)view.traitCollection.userInterfaceStyle; + } + } + return rasterContentsCache()->objectForKey({userInterfaceStyle, _renderer.attributes, _renderer.constrainedSize}); } - (void)didDisplayAsynchronously:(id)newContents withDrawParameters:(id)drawParameters { if (newContents) { + NSInteger userInterfaceStyle = 1; + if (@available(iOS 13.0, *)) { + UIView *view = (UIView *)self.delegate; + if ([view isKindOfClass:UIView.class]) { + userInterfaceStyle = (NSInteger)view.traitCollection.userInterfaceStyle; + } + } + CGImageRef imageRef = (__bridge CGImageRef)newContents; NSUInteger bytes = CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef); - rasterContentsCache()->cacheObject({_renderer.attributes, _renderer.constrainedSize}, newContents, bytes); + rasterContentsCache()->cacheObject({userInterfaceStyle, _renderer.attributes, _renderer.constrainedSize}, newContents, bytes); } } diff --git a/ComponentTextKit/CKTextComponentView.mm b/ComponentTextKit/CKTextComponentView.mm index ae41d36ec..cdf9d47a9 100644 --- a/ComponentTextKit/CKTextComponentView.mm +++ b/ComponentTextKit/CKTextComponentView.mm @@ -42,6 +42,17 @@ - (instancetype)initWithFrame:(CGRect)frame return self; } +- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection +{ + [super traitCollectionDidChange:previousTraitCollection]; + + if (@available(iOS 13.0, *)) { + if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) { + [self.layer setNeedsDisplay]; + } + } +} + - (void)setBackgroundColor:(UIColor *)backgroundColor { if (![self.backgroundColor isEqual:backgroundColor]) { diff --git a/ComponentTextKit/TextKit/CKTextKitRendererCache.h b/ComponentTextKit/TextKit/CKTextKitRendererCache.h index 71769802c..5d307f784 100644 --- a/ComponentTextKit/TextKit/CKTextKitRendererCache.h +++ b/ComponentTextKit/TextKit/CKTextKitRendererCache.h @@ -67,10 +67,11 @@ namespace CK { */ struct Key { + NSInteger userInterfaceStyle; CKTextKitAttributes attributes; CGSize constrainedSize; - Key(CKTextKitAttributes a, CGSize cs); + Key(NSInteger userInterfaceStyle, CKTextKitAttributes a, CGSize cs); size_t hash; @@ -79,7 +80,8 @@ namespace CK { // These comparisons are in a specific order to reduce the overall cost of this function. return hash == other.hash && CGSizeEqualToSize(constrainedSize, other.constrainedSize) - && attributes == other.attributes; + && attributes == other.attributes + && userInterfaceStyle == other.userInterfaceStyle; } }; diff --git a/ComponentTextKit/TextKit/CKTextKitRendererCache.mm b/ComponentTextKit/TextKit/CKTextKitRendererCache.mm index d17fb9b02..dfb592e3f 100644 --- a/ComponentTextKit/TextKit/CKTextKitRendererCache.mm +++ b/ComponentTextKit/TextKit/CKTextKitRendererCache.mm @@ -26,12 +26,13 @@ void enteredBackgroundNotificationHandler(CFNotificationCenterRef center, void * } namespace Renderer { - Key::Key(CKTextKitAttributes a, CGSize cs) : attributes(a), constrainedSize(cs) { + Key::Key(NSInteger u, CKTextKitAttributes a, CGSize cs) : userInterfaceStyle(u), attributes(a), constrainedSize(cs) { // Precompute hash to avoid paying cost every time getHash is called. NSUInteger subhashes[] = { attributes.hash(), std::hash()(constrainedSize.width), - std::hash()(constrainedSize.height) + std::hash()(constrainedSize.height), + std::hash()(userInterfaceStyle) }; hash = RCIntegerArrayHash(subhashes, CK_ARRAY_COUNT(subhashes)); }