How to Change Font Family for textInput Placeholder in React Native - react-native

I have to change the font family for textInput's Placeholder text. If we add this secureTextEntry={true}, the mentioned font Family is set for placeholder text.
<TextInput style={styles.textboxfield} secureTextEntry={true} placeholder="Password" />
But if we remove this secureTextEntry={true}, we can't set font-family for placeholder
<TextInput style={styles.textboxfield} placeholder="Password" />
Style is : textboxfieldd: {
height: 39,
backgroundColor: '#ffffff',
marginBottom:0,
fontFamily:'Inconsolata-Regular',
},
How can I change the font family for placeholder text ?

The way I solved this was to conditionally style the fontFamily (or style) on the presence or absence of the value i.e.
<TextInput
style={{ fontFamily: value ? 'OpenSans-Regular' : 'OpenSans-Italic' }}
value={value}
onChangeText={onChange}
/>
This way the font family is italic for the placeholder (when value === '') and regular when text is shown.
Above is not tested as I was using styled components but concept should be the same. Also this only works if TextInput is rendered as a controlled component so you have access to value.

Try this :
<TextInput
secureTextEntry={(this.state.email.length <= 0 && this.state.emailStatus != 'onFocus') ? true : false}
style={styles.textboxfieldd}
placeholderStyle={styles.textboxfieldd}
onFocus={this.changeStatus.bind(this, 'emailStatus', 'onFocus', '')}
onChangeText={(email) => this.setState({ email })}
value={this.state.email}
placeholder={this.state.emailStatusPH}
placeholderTextColor="#D8D8D8" />
Exactly this line => secureTextEntry={(this.state.email.length<=0 && this.state.emailStatus!='onFocus') ?true:false} solves the problem .
Because if we give secureTextEntry={true} means fontfamily is set to placeholder text but field changed as password , so for that only we wrote like this . secureTextEntry={(this.state.email.length<=0 && this.state.emailStatus!='onFocus') ?true:false}
If that field length is 0 and not focused means it will set true secureTextEntry={true} so the placeholder text is set to mentioned fontfamily

Textinput can have a Text child.
So you can have
<TextInput>
<Text style = {value.length == 0? styles.hint : styles.input}>
{value.length == 0? hint : value}
</Text>
</TextInput>

You can handle this by length of your password like :
<TextInput
secureTextEntry={this.state.password.length === 0? false : true}
/>

If you want to change the font once, you can just set fontFamily: yourFontFamilyName
If you plan on using your font in many place I suggest you create a class that will use the same fontFamily everyTime :
You can do it this way : (example with Quicksand as font-family)
import React, {TextInput} from 'react-native';
import _ from 'lodash';
var OldTextInput = TextInput;
class NewTextInput extends OldTextInput {
defaultProps = {};
render() {
var props = _.clone(this.props);
if (_.isArray(this.props.style)){
props.style.push({fontFamily: 'Quicksand-Regular'});
} else if (props.style) {
props.style = [props.style, {fontFamily: 'Quicksand-Regular'}];
} else {
props.style = {fontFamily: 'Quicksand-Regular'};
}
this.props = props;
return super.render();
};
}
export default NewTextInput;
and then use TextInput by requiring it in every file (import TextInput from './TextInput')

You're likely experiencing this on Android with React Native <= 0.64. In which case here's a solution:
<TextInput ref={ref => ref && ref.setNativeProps({ style: { fontFamily: 'FONT_NAME' } })} />
For more information and alternatives that might work better for you look at this issue thread on Github

Add fontSize={20}
<TextInput style={styles.textboxfield} secureTextEntry={true} placeholder="Password" fontSize={20}
/>

its worked for me
fontWeight: 'normal',
good luck :)

Related

React Native TextInput the input text is not displayed

React Native When I input TextInput, the input text is not displayed. This is an accidental situation. How can I solve this problem?
<TextInput
style={styles.text_input}
placeholder={'请填写注册时的手机号'}
underlineColorAndroid='transparent'
returnKeyType ='done'
onChangeText={(text)=> {
this.setState({
phoneNumber: text
});
}}
/>
text_input: {
fontSize: 14,
height: 40,
color:Colors.FontBlackColor
}
Question screenshot
Looks like you're missing the value prop.
<TextInput
onChangeText={(text)=> {
this.setState({
phoneNumber: text
});
}}
value={this.state.phoneNumber} // Add this prop
/>
You have to give TextInput the value prop:
<TextInput value={"string"} placeholder='textinput' />

Don't grey out Disabled Input in React Native Elements

I am trying to change the color of disabled React-native-elements component Input. Default behaviour is to grey out everything but I would like to have solid black color of the text even if it's disabled. Has anybody tips of how to do it?
Set disabledInputStyle prop to opacity: 1
disabledInputStyle={{opacity: 1}}
I first read the official API and find the disabledInputStyle, then I look at the Input source in react-naive-element.
...
Input.defaultProps = {
InputComponent: TextInput,
};
...
// here find it defalut use textinput in react-native,and when disable true,use the disalbeInputStyle
render(){
<View style={StyleSheet.flatten([styles.container, containerStyle])}>
....
<InputComponent
testID="RNE__Input__text-input"
underlineColorAndroid="transparent"
editable={!disabled}
{...patchWebProps(attributes)}
ref={ref => {
this.input = ref;
}}
style={StyleSheet.flatten([
styles.input,
inputStyle,
disabled && styles.disabledInput,
disabled && disabledInputStyle,
])}
/>
...
</View>
}
For the TextInput in react-native, we set the text color used color style
so you can try to do use disabledInputStyle, and set the color you want.
<Input
disabled={true}
value={"ddd"}
disabledInputStyle={{color:'red',opacity:1}} //chanage which color you want
placeholder='INPUT WITH ERROR MESSAGE'
errorStyle={{ color: 'red' }}
errorMessage='ENTER A VALID ERROR HERE'
/>

React Native : TextInput loosing text when style changes

I have a TextInput whose style changes according to a state.
<TextInput
autoCapitalize="characters"
defaultValue={this.props.name}
onChangeText={(text) => { this.handleChangeText(text); }}
onEndEditing={this.handleTextSubmit}
placeholder={this.props.dictionary.Identification__placeholder}
placeholderTextColor={color.WHITE}
ref={(component) => { this.textInput = component; }}
style={[
defaultStyles.input,
styles.input,
this.state.keyboardIsVisible && styles.inputSelected,
]}
underlineColorAndroid="rgba(0,0,0,0)"
/>
The problem is that when the style changes, it erases the current value in the TextInput. Is there a way to fix this behavior ?
What is handleTextChange doing? you should have a value property like value={this.state.inputText} and have handleTextChange update that value.

Is there a way to do letterSpacing for TextInput in React Native?

I've been trying to give <TextInput> a letterSpacing style but it seems it only works for <Text>. Is there a way in React Native to increase the space between characters in TextInput?
This increases the space between characters
<Text style={{'letterSpacing': 5}}>Some Text</Text>
This won't work
<TextInput style={{'letterSpacing': 5}} />
I think this is a hack - nevertheless, it works:
<TextInput letterSpacing={5} ... />
I have asked here on SO why style keys also work as props on components, still no answer.
Do not consider this solution permanent, this is obviously not the intended way to style components in RN.
This is a possible solution, maybe not the better.
Jsfiddle
class Form extends React.Component {
constructor() {
super();
this.state = {
value: '',
};
this.onChangeText = this.onChangeText.bind(this);
}
onChangeText(e) {
const val = e.target.value.replace(/ /g, '').split('');
const spaced = val.join(' ');
this.setState({
value: spaced,
});
}
render() {
return(
<div style={{flexDirection: 'column'}}>
<div>Type Text</div>
<input
type='text'
onChange={this.onChangeText}
value={this.state.value}
/>
</div>
);
}
}
You should give letterSpacing inside inputStyle prop.
Example:
<TextInput inputStyle={{ letterSpacing: 20 }}/>
Check out this component
https://github.com/wix/react-native-ui-lib#masked-input
it allows you to render custom masked input in any way you want it, without affecting the actual value of the input.
just put text inside textInput and style it like below
<TextInput
placeholder={'1 2 3 4 5 6'}
style={textInputViewStyle}
onChangeText={setCode}
maxLength={6}
>
<Text style={{ letterSpacing: 19 }}>{code}</Text>
</TextInput>

ReactNative TextInput placeholderTextColor doesn't seem to be working

It seems like such a simple thing, I don't see how I'm not getting this right, but placeholderTextColor on a ReactNative TextInput isn't doing anything for me.
http://facebook.github.io/react-native/docs/textinput.html#placeholdertextcolor
<TextInput
style={styles.input}
placeholder="Foobar"
placeholderTextColor="#FFFFFF"/>
does nothing....
This works -
<TextInput
placeholder="Enter password"
placeholderTextColor="white"
/>
Hope it helps! Cheers!
<TextInput
value={this.state.environment}
onChangeText={(environment) => this.setState({ environment })}
placeholder={'Environment'}
placeholderTextColor="#202020"
secureTextEntry={true}
style={styles.input}
underlineColorAndroid='rgba(0,0,0,0)'
/>
I did this and it works quite well:
// use standard input
import { TextInput } from 'react-native';
<TextInput
style={[styles.searchInput, { opacity: this.state.location.length ? 1 : 0.6 }]}
placeholder="Location"
placeholderTextColor={colors.lighterGrey}
onChangeText={location => this.setState({ location })}
value={this.state.location}
/>
const styles = StyleSheet.create({
searchInput: {
flex: 1,
lineHeight: 22,
fontSize: 17,
fontFamily: fonts.secondary.regular,
color: colors.white,
padding: 5,
},
});
I was having issues getting the opacity to work, so I found the above solution worked well as a minimal markup solution:
style={[styles.searchInput, { opacity: this.state.location.length ? 1 : 0.6 }]}
The style prop can accept an Array of style Objects which has right to left precedence, so you can override default styles with state-based addons. I might also say it is related to CSS specificity.
In my case, I was having trouble modulating the text colours between the placeholder text and 'filled-in text. The opacity of styles.searchInput was affecting the <TextInput placeholderTextColor=""> prop.
My solution here handles the opacity concern on both iOS and Android and demonstrates a pretty normal <TextInput> setup.
In the context of my example code above, a person can always examine this.state.location.length to see if the field is empty or not.
If so, use TextInput's style prop with computed props (same as in Vue.js). You could drop a function in there, like:
style={[styles.textInput, calculatedStyles()]}
Don't forget you can spread in Objects also:
style={[...inputStyles, ...errorStyles]}
As Asanka Sampath shows in another answer, depending on your design, underlineColorAndroid can be a very useful TextInput prop.
placeholder: "Enter password",
placeholderTextColor: "white"
try this in Latest Version in React Native
Simple and easy solution
<TextInput
style={styles.textInputStyle} //if you want give style to the TextInput
placeholder="Email"
placeholderTextColor="steelblue"
/>