答案 0 :(得分:3)
我建议您对功能组件使用自定义样式。创建一个名为RenderInput
的功能组件,为此将placeholder
作为道具。
import React, {Component} from 'react';
import {TextInput, View, Text} from 'react-native';
const RenderInput = ({ label , inputvalue,styleLabel, ipOnChangeText, placeholder}) => {
const {inputStyle, labelStyle, containerStyle} = styles;
return(
<View style = {containerStyle}>
<Text style= {styleLabel ? labelStyle : ""} >{label}</Text>
<TextInput
autoCorrect={false}
placeholder={placeholder}
style= {inputStyle}
/>
</View>
);
}
const styles ={
inputStyle:{
color: '#333',
fontSize: 16,
lineHeight: 23,
borderBottomColor: '#333',
borderBottomWidth: 0.5,
fontFamily: 'System',
},
labelStyle:{
fontSize: 18,
color: '#737373',
paddingBottom: 10,
fontFamily: 'System',
position: 'relative',
':after': {
content: '* ',
position: absolute,
left: 5,
top: 0,
color: '#bbb'
}
},
containerStyle:{
flexDirection: 'column',
marginTop: 10,
marginBottom: 10
}
}
export { RenderInput };
答案 1 :(得分:1)
创建一个名称为MyTextInput的组件,如下所示:
class MyTextInput extends Component {
constructor(props) {
super(props);
this.state = { placeholder: props.text.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}
/>
}
}
并在另一个组件中使用它:
import MyTextInput from '.../MyTextInput';
<MyTextInput
text={this.state.myText}
style={{ fontFamily: "Your Font" }}
placeholderStyle={{ fontFamily: "AnotherFont", borderColor: 'red' }}
/>
您可以通过这种方式自定义所需的任何组件。
答案 2 :(得分:0)
您可以这样做
<TextInput
placeholder="YOUR TEXT"
placeholderTextColor="#555555"
/>
当然,您也可以创建自己的TextInput组件版本,该组件应在TextInput顶部包含自定义占位符,一旦文本更改,您将隐藏自定义占位符