跳至主内容

PlatformColor

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

js
PlatformColor(color1, [color2, ...colorN]);

使用 PlatformColor 函数,通过传入原生颜色对应的字符串值,可以访问目标平台上的原生颜色。你只需向 PlatformColor 函数传递一个字符串,如果该字符串在目标平台上存在,函数将返回对应的原生颜色,你可以在应用的任何部分使用它。

如果向 PlatformColor 函数传递了多个字符串值,函数会将第一个值视为默认值,其余值作为备选值。

js
PlatformColor('bogusName', 'linkColor');

由于原生颜色可能受到主题和/或高对比度设置的影响,这种平台特定的逻辑也会在你的组件内部生效。

支持的颜色

完整支持的系统颜色类型列表,请参阅:

开发者说明

信息

If you’re familiar with design systems, another way of thinking about this is that PlatformColor lets you tap into the local design system's color tokens so your app can blend right in!

示例

传递给 PlatformColor 函数的字符串值必须与应用程序运行的原生平台上的字符串完全匹配。为避免运行时错误,该函数应包裹在平台检查中,可以使用 Platform.OS === 'platform'Platform.select() 方法,如上例所示。

备注

你可以在 PlatformColorExample.js 中找到展示如何正确使用 PlatformColor 的完整示例。