Skip to content

Commit a2f8b9c

Browse files
JonnyBurgerfacebook-github-bot
authored andcommitted
Set black as default text color for <TextInput/> on iOS (#28708)
Summary: This is a follow-up pull request to #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: #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
1 parent d06ee3d commit a2f8b9c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Libraries/Text/TextInput/RCTBaseTextInputView.m

+7-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ - (void)setTextAttributes:(RCTTextAttributes *)textAttributes
6868
- (void)enforceTextAttributesIfNeeded
6969
{
7070
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
71-
backedTextInputView.defaultTextAttributes = [_textAttributes effectiveTextAttributes];
71+
72+
NSDictionary<NSAttributedStringKey,id> *textAttributes = [[_textAttributes effectiveTextAttributes] mutableCopy];
73+
if ([textAttributes valueForKey:NSForegroundColorAttributeName] == nil) {
74+
[textAttributes setValue:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
75+
}
76+
77+
backedTextInputView.defaultTextAttributes = textAttributes;
7278
}
7379

7480
- (void)setReactPaddingInsets:(UIEdgeInsets)reactPaddingInsets

0 commit comments

Comments
 (0)