[英]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.