Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Font Family for textInput Placeholder in React Native

Tags:

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 ?

like image 255
Thivya Avatar asked Feb 29 '16 13:02

Thivya


People also ask

How do I change my placeholder font family?

When you apply a CSS font styles to the input field, the placeholder text will also inherit that properties. But if you only want to change the font styles of the placeholder text but not the input itself, you'll need a special pseduo-element.

How do I change the placeholder text size in React Native?

You can modify text size of placeholder by changing the fontSize through Stylesheet object.

How do I add a placeholder in TextInput in React Native?

Basically Placeholder provides hints or message about that particular TextInput Component, To add PlaceHolder text in TextInput Component, you need to specify placeholder=" Enter Your First Name" Props inside the TextInput Component.


2 Answers

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.

like image 189
Jon Wyatt Avatar answered Sep 19 '22 19:09

Jon Wyatt


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

like image 40
Thivya Avatar answered Sep 16 '22 19:09

Thivya