如何在 React Native 中更改 TextInput 占位符的样式?

新手上路,请多包涵

有没有办法为 React Native 中的 TextInput 的 placeholder 设置 fontStyle: 'italic'

这里的文档 似乎只能设置占位符和占位符TextColor。

原文由 Zohar Levin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 587
2 个回答

改进丹尼尔的答案以获得更通用的解决方案

class TextInput2 extends Component {
  constructor(props) {
    super(props);
    this.state = { placeholder: props.value.length == 0 }
    this.handleChange = this.handleChange.bind(this);
  }
  handleChange(ev) {
    this.setState({ placeholder: ev.nativeEvent.text.length == 0 });
    this.props.onChange && this.props.onChange(ev);
  }
  render() {
    const { placeholderStyle, style, onChange, ...rest } = this.props;

    return <TextInput
      {...rest}
      onChange={this.handleChange}
      style={this.state.placeholder ? [style, placeholderStyle] : style}
      />
  }
}

用法:

 <TextInput2
  value={this.state.myText}
  style={{ fontFamily: "MyFont" }}
  placeholderStyle={{ fontFamily: "AnotherFont", borderColor: 'red' }}
/>

原文由 Jake Coxon 发布,翻译遵循 CC BY-SA 4.0 许可协议

传入一个组件作为占位符对我有用

原文由 sandypockets 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题