简体   繁体   English

如何在文本输入的占位符中给出多个 colors 反应原生

[英]how to give multiple colors in placeholder of textinput react native

see image看图片

In react native text input placeholder, is there any way to give multiple colors to place holder text.在反应原生文本输入占位符中,有没有办法给多个 colors 来占位符文本。 email[black color] asterisk [red color]电子邮件[黑色] 星号 [红色]

I have tried placeholdertextcolor property of text input.我试过文本输入的 placeholdertextcolor 属性。 But it will allow only one color for whole placeholder text.但它只允许整个占位符文本使用一种颜色。

The image in the link is my required textinput UI.链接中的图像是我需要的文本输入 UI。

You can make your own placeholder component wrapped inside the TextInput component.您可以将自己的占位符组件包装在 TextInput 组件中。 It will look something similar to this它看起来与此类似

       <TextInput
        value={value}
        isFocused={isFocused}
        onFocus={() =>
          this.setState({ isFocused: true }, () => onFocus && onFocus())
        }
        onBlur={() => this.setState({ isFocused: false })}
      >
        {!isFocused && !value && (
          <Text>
            Placeholder Text<Text style={{ color: 'red' }}>*</Text>
          </Text>
        )}
      </TextInput>

Make sure to keep track of the focused state of your input component so that you can display placeholder or no.确保跟踪输入组件的焦点 state,以便您可以显示占位符或否。

As mentioned in the previous comment you can add your custom placeholder, you can also pass it to the state and clear it when press on the text input using onResponderStart,here is full example:如上一条评论所述,您可以添加自定义占位符,也可以将其传递给 state 并在使用 onResponderStart 按下文本输入时将其清除,这是完整示例:

const example = () => {
  const [placeHolderText, setPlaceHolderText] = React.useState('PlaceHolder');
  const [placeHolderOtherText, setPlaceHolderOtherText] = React.useState('*');

  return (
    <View style={{flex: 1, alignContent: 'center', justifyContent: 'center'}}>
      <TextInput
        //remove placeholder on touching
        onResponderStart={() => {
          setPlaceHolderOtherText('');
          setPlaceHolderText('');
        }}>
        <Text>
          {placeHolderText}
          <Text style={{color: 'red'}}>{placeHolderOtherText}</Text>
        </Text>
      </TextInput>
    </View>
  );
};

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

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