Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ - (void)didMoveToWindow
[self _restoreTextSelection];
}

// [macOS
- (void)_updateDefaultTextAttributes
{
const auto &props = static_cast<const TextInputProps &>(*_props);
NSMutableDictionary<NSAttributedStringKey, id> *attrs =
RCTNSTextAttributesFromTextAttributes(props.getEffectiveTextAttributes(RCTFontSizeMultiplier()));

_backedTextInputView.defaultTextAttributes = attrs;

// Also update the existing attributed text so the visible text re-renders
// with the new color (defaultTextAttributes only affects newly typed text).
// Wrap in _comingFromJS to prevent textInputDidChange from pushing a state
// update back to the shadow tree, which would overwrite our fresh colors
// with the stale cached attributed string.
NSString *currentText = _backedTextInputView.attributedText.string;
if (currentText.length > 0) {
NSAttributedString *updated = [[NSAttributedString alloc] initWithString:currentText attributes:attrs];
_comingFromJS = YES;
_backedTextInputView.attributedText = updated;
_comingFromJS = NO;
}
}
// macOS]

#if !TARGET_OS_OSX // [macOS]
// TODO: replace with registerForTraitChanges once iOS 17.0 is the lowest supported version
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
Expand All @@ -183,8 +207,20 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
_backedTextInputView.defaultTextAttributes =
RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier()));
}

// [macOS
if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
[self _updateDefaultTextAttributes];
}
// macOS]
}
#else // [macOS
- (void)viewDidChangeEffectiveAppearance
{
[super viewDidChangeEffectiveAppearance];
[self _updateDefaultTextAttributes];
}
#endif // [macOS]
#endif // macOS]

- (void)reactUpdateResponderOffsetForScrollView:(RCTScrollViewComponentView *)scrollView
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
{
RCTUIColor *effectiveForegroundColor = RCTUIColorFromSharedColor(textAttributes.foregroundColor) ?: [RCTUIColor labelColor]; // [macOS]

#if TARGET_OS_OSX // [macOS
// On macOS, colorWithAlphaComponent: converts dynamic system colors (like
// NSColor.labelColor) to static resolved colors, preventing them from
// adapting to appearance changes. Skip when opacity is 1.0 (a no-op).
if (!isnan(textAttributes.opacity) && textAttributes.opacity != 1.0f) {
#else // macOS]
if (!isnan(textAttributes.opacity)) {
#endif
effectiveForegroundColor = [effectiveForegroundColor
colorWithAlphaComponent:CGColorGetAlpha(effectiveForegroundColor.CGColor) * textAttributes.opacity];
}
Expand All @@ -158,7 +165,11 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
{
RCTUIColor *effectiveBackgroundColor = RCTUIColorFromSharedColor(textAttributes.backgroundColor); // [macOS]

#if TARGET_OS_OSX // [macOS
if (effectiveBackgroundColor && !isnan(textAttributes.opacity) && textAttributes.opacity != 1.0f) {
#else // macOS]
if (effectiveBackgroundColor && !isnan(textAttributes.opacity)) {
#endif
effectiveBackgroundColor = [effectiveBackgroundColor
colorWithAlphaComponent:CGColorGetAlpha(effectiveBackgroundColor.CGColor) * textAttributes.opacity];
}
Expand Down
Loading