From a2f8b9c36e5eba6bc354a2f53bf8d3ca11297d00 Mon Sep 17 00:00:00 2001 From: Jonny Burger Date: Wed, 22 Apr 2020 23:40:59 -0700 Subject: [PATCH] Set black as default text color for on iOS (#28708) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This is a follow-up pull request to https://github.com/facebook/react-native/issues/28280 (reviewed by shergin). This pull request tried to solve the problem of the default color in a TextInput in dark mode on iOS being white instead of black. I got suggested to solve the problem not on the level of RCTTextAttributes, but on the level of RCTUITextField. Setting `self.textColor = [UIColor black];` in the constructor did not work, because it gets overwritten by nil in `RCTBaseTextInputView.m`. There I implemented the logic that if NSForegroundColorAttributeName color is nil then the color is being set to black. I think the `defaultTextAttributes` property confuses here, because it ends up being the effective text attributes, e.g. if I unconditionally set the default text color to black, it cannot be changed in React Native anymore. So I put the nil check in. ## Changelog [iOS] [Fixed] - TextInput color has the same default (#000) on iOS whether in light or dark mode Pull Request resolved: https://github.com/facebook/react-native/pull/28708 Test Plan: I have manually tested the following: - The default text color in light mode is black - The default text color in dark mode is black - The color can be changed using the `style.color` attribute - Setting the opacity to 0.5 results in the desired behavior, the whole TextInput becoming half the opacity. – Setting the `style.color` to rgba(0, 0, 0, 0.5) works as intended, creating a half-opaque text color. Differential Revision: D21186579 Pulled By: shergin fbshipit-source-id: ea6405ac6a0243c96677335169b214a2bb9ccc29 --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 114bc21478beb4..538832e0e92bcc 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -68,7 +68,13 @@ - (void)setTextAttributes:(RCTTextAttributes *)textAttributes - (void)enforceTextAttributesIfNeeded { id backedTextInputView = self.backedTextInputView; - backedTextInputView.defaultTextAttributes = [_textAttributes effectiveTextAttributes]; + + NSDictionary *textAttributes = [[_textAttributes effectiveTextAttributes] mutableCopy]; + if ([textAttributes valueForKey:NSForegroundColorAttributeName] == nil) { + [textAttributes setValue:[UIColor blackColor] forKey:NSForegroundColorAttributeName]; + } + + backedTextInputView.defaultTextAttributes = textAttributes; } - (void)setReactPaddingInsets:(UIEdgeInsets)reactPaddingInsets