简体   繁体   English

如何在深色模式和浅色模式之间更改文本颜色

[英]How to change text color between dark mode and light mode

I have a basic text like this in my react native app我的反应原生应用程序中有这样的基本文本

<Text style={{color:"#000000"}}>Text on IOS</Text>

What i want to do is to switch color to white when the phone gets switched to dark mode for IOS and switch it back to black if mode changed again to light and first how to active that if possible without restarting the app and without affecting android.我想要做的是当手机切换到 IOS 的暗模式时将颜色切换为白色,如果模式再次变为亮,则将其切换回黑色,首先如何在不重新启动应用程序且不影响 android 的情况下激活它。 second how to detect that the mode has been switched to dark or light.第二如何检测模式已切换到暗或亮。

React-native now has hook useColorScheme which you can use to track theme changes React-native 现在有钩子useColorScheme ,您可以使用它来跟踪主题更改

import { useColorScheme } from 'react-native';

const App = () => {
  const scheme = useColorScheme();
  return (<Text style={{ color: scheme === 'dark' ? 'white' : 'black'}}>Hello World</Text>);
};

If you do not use hooks and functional components, you can use Appearance class and listen for changes yourself:如果不使用钩子和功能组件,可以使用Appearance class 并自己监听变化:

import { Appearance } from 'react-native';
...
// get current scheme
Appearance.getColorScheme()

// listen for changes
Appearance.addChangeListener(({ colorScheme }) => console.log(colorScheme);

Try to use a global variable for setting theme.尝试使用全局变量来设置主题。 Create a common file and get the styles from there using global variable.创建一个通用文件并使用全局变量从那里获取 styles。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据暗/亮模式设置默认 label 颜色(在 Swift 中) - How to set default label color based on dark/light mode (in Swift) 如何使用 SwiftUI 为明暗模式之间的过渡设置动画? - How to animate the transition between light and dark mode using SwiftUI? 如何在 Xamarin.iOS 中的明暗模式之间切换? - How to toggle between Light & Dark Mode in Xamarin.iOS? 从亮模式到暗模式的颜色变化在真实设备上的 SwiftUI 中不起作用 - Color change from light-mode to dark-mode not working in SwiftUI on real device SwiftUI 使用@AppStorage 更改暗/亮模式 - SwiftUI dark / light mode change with @AppStorage SwiftUI:获取动态背景颜色(暗模式或亮模式) - SwiftUI: Get the Dynamic Background Color (Dark Mode or Light Mode) iOS 浅色模式和深色模式 - iOS light mode and dark mode 为深色模式更改 UIDatePicker 选定的日期文本颜色(iOS 13) - Change UIDatePicker selected Date text Color for Dark mode (iOS 13) 如何在 iOS 的原生反应中动态地将亮模式更改为暗模式? - how do I dynamically change light mode to dark mode in react native in iOS? 如果设备处于深色模式,如何将 iOS 中的文本指定为白色,如果处于浅色模式,如何将文本指定为黑色? - How can I specify text in iOS to be white if the device is in dark mode and black if in light mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM