繁体   English   中英

React Native - 根据背景颜色更改文本颜色

[英]React Native - Change text color according to background color

我想做如下图所示的操作,但我不知道如何实现(我用谷歌搜索,但我只找到了本机代码 Swift、Obj C 等的结果)。

在此处输入图片说明

我是否必须玩一些图层或类似的东西?

感谢您的回答!

维克多

这个实现有点不同,主要是我有 2 个不同样式的不同视图,最后,使用主视图包装这 2 个视图。

import * as React from "react";
import { Text, View, StyleSheet } from "react-native";

export default class Example extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.leftViewStyle}>
          <Text style={styles.leftLabelStyle}>Text color ch</Text>
        </View>
        <View style={styles.rightViewStyle}>
          <Text style={styles.rigthLabelStyle}>ange</Text>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center",
    marginHorizontal: 20
  },
  leftViewStyle: {
    flex: 1,
    backgroundColor: "#5483b3",
    alignItems: "flex-end",
    borderTopLeftRadius: 20,
    borderBottomLeftRadius: 20
  },
  rightViewStyle: {
    flex: 1,
    backgroundColor: "#d0d3d6",
    borderTopRightRadius: 20,
    borderBottomRightRadius: 20
  },
  leftLabelStyle: {
    fontSize: 16,
    paddingVertical: 5,
    color: "#fff"
  },
  rigthLabelStyle: {
    fontSize: 16,
    paddingVertical: 5,
    color: "#000"
  }
});

这可能不是最佳解决方案。 如果您有任何疑问,请随时提出。 希望这会帮助你。

与上一个答案相同的样式,但通过添加另一个包含相同文本的视图来更改实现,该视图包含具有不同背景颜色和文本颜色的灰色视图,位置为'absolute'

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';

export default class Example extends React.Component {
  state = {
    text: 'Text color changes',
  };

  render() {
    console.log(this.state.textArray);
    return (
      <View style={styles.container}>
        <View
          style={{
            flex: 1,
            backgroundColor: '#d0d3d6',
            borderTopRightRadius: 20,
            borderBottomRightRadius: 20,
            borderTopLeftRadius: 20,
            borderBottomLeftRadius: 20,
            overflow: 'hidden',
          }}>
          <Text style={styles.leftLabelStyle}>{this.state.text}</Text>
          <View
            style={{
              width: '30%',
              height: '100%',
              position: 'absolute',
              backgroundColor: '#5483b3',
            }}>
            <Text numberOfLines={1} ellipsizeMode='clip' 
              {styles.RightLabelStyle}>
              {this.state.text}
            </Text>
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    marginHorizontal: 20,
  },

  leftLabelStyle: {
    fontSize: 16,
    paddingVertical: 5,
    color: '#000',
  },
  RightLabelStyle: {
    fontSize: 16,
    paddingVertical: 5,
    color: '#fff',
  },
});

小吃示例: https : //snack.expo.io/@hassan190011/loading

编辑:在 numberOfLines 之后添加ellipsizeMode='clip'以删除点

嘿,非常感谢你的回答,

我不得不用省略号处理文本溢出,而且你提出的内容真的很复杂,所以我使用了 maskView 来做到这一点。

我是这样的:

// container
<View style={{flex:1, borderRadius: 200, height: 25, overflow:hidden}}>

    // Progress bar support
    <View style={{flex:1, backgroundColor: 'gray'}} />

    // Progress bar, I play with "width" to indicate percentage
    <View style={[StyleSheet.absoluteFill, {width: "50%", backgroundColor: 'green'}]} />
    <MaskedView
        style={[StyleSheet.absoluteFill, {justifyContent: 'center'}]}
        maskElement={
            // I define text which will be masked
            <View style={[StyleSheet.absoluteFill, {justifyContent: 'center'}]}>
                <Text
                    style={{marginHorizontal: 15, fontSize: 13}}
                    numberOfLines={1}>
                    Text color change
                </Text>
            </View>
        }>

        // I define a default mask that I apply to the text so that it is 'black' when it is not ON the progress bar.
        <View style={[StyleSheet.absoluteFill,{backgroundColor: 'black'}]} />

        // I define the mask that takes the size of the progress bar and that I apply over the default mask (to overwrite it) so that the text under the mask becomes white.
        <View style={[StyleSheet.absoluteFill,{width:"50%", backgroundColor: '#fff'}]} />
    </MaskedView>
</View>

所以我有我的默认栏指示“最大进度”,我定义了我的进度栏(根据百分比增长或缩小)。 然后我用我的文本定义一个 MaskedView 作为 MaskedElement。 默认情况下,我在文本上应用黑色蒙版,以便无论发生什么情况它始终是黑色的。 然后,我用与我的进度条大小完全相同的白色蒙版覆盖了这个蒙版。 所以进度条遮罩下的所有内容都变成白色以显示在我的“黑暗”进度条上,其余部分为黑色!

这样,我可以使用 ellpsizeMode="tail" 轻松管理文本溢出。

这是结果:

结果1

结果 2

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM