简体   繁体   English

如何更改 react-native 中的占位符文本大小

[英]how to change placeholder textSize in react-native

In my TextInput I want to change the size of the font of placeHolder , I tried like this:在我的TextInput中,我想更改placeHolder字体的大小,我试过这样:

<TextInput
    style={styles.email}
    value={this.state.email} 
    placeholder="Email"
    placeholderTextColor="red"
    //placeholderTextSize = 20px
    onChange={this.handleEmailChange.bind(this)}
    onBlur={this.onBlur.bind(this)}/>

when I wrote like this color is working for me but size is not working, can any one give me suggestions how to change the size of placeholder Text, any help much appreciated当我写这样的颜色对我有用但大小不起作用时,任何人都可以给我建议如何更改占位符文本的大小,非常感谢任何帮助

I am not sure if you can specify only the size of placeholderText.我不确定您是否只能指定 placeholderText 的大小。 But you can change the fontSize of the input text from your TextInput:但是您可以从 TextInput 更改输入文本的 fontSize:

var styles = StyleSheet.create({
 email:     {
    fontSize:   12, <-- Textsize of Input-Text and Placeholder-Text
 },
})

Maybe your solution works without px?!也许您的解决方案在没有 px 的情况下有效?! But i don´t think so.但我不这么认为。 If you want to have a different PlaceholderText than the InputText, than you could maybe acces the onChangeText-Method of TextInput and change the fontSize as soon as you type something into the Textfield.如果您想要一个与 InputText 不同的 PlaceholderText,那么您可以访问 TextInput 的 onChangeText-Method 并在您在 Textfield 中输入内容后立即更改 fontSize。

<TextInput 
style={{fontSize:12}} 
placeholder='any text' 
onChangeText={(input)=>this.setState({input: input})}
placeholderTextColor='green'/>

There's no way (as far as I remember) to directly change the font size of only the placeholder, since when you change the fontSize using styles, you're changing both, the input fontSize as well as placeholder fontSize.没有办法(据我所知)直接更改占位符的字体大小,因为当您使用样式更改 fontSize 时,您同时更改了输入 fontSize 和占位符 fontSize。 However, there is a way tho...然而,有一种方法可以...

const [ text, setText ] = useState<string>('');

and then use the state to check whether the input field is empty or not.然后使用状态检查输入字段是否为空。 If it is empty, then change the fontSize as:如果为空,则将 fontSize 更改为:

{ fontSize: text ? 18 : 12 }

However, it is important to note that even tho this method works, but the delay caused by the re-render is, of course, noticable.然而,重要的是要注意,即使这种方法有效,但由重新渲染引起的延迟当然是值得注意的。

TextInput component can be created like this. TextInput 组件可以这样创建。

<TextInput
value={value}
style={value ? styles.input : styles.placeholder}
placeholderTextColor="white"
{...props}/>

Just need to increase the 'fontSize' property of TextInput.只需要增加TextInput 的'fontSize' 属性。 Then the placeholder size will automatically get increase然后占位符大小会自动增加

here is your code这是你的代码

<TextInput
    style={styles.email}
    value={this.state.email} 
    placeholder="Email"
    placeholderTextColor="red"
    //placeholderTextSize = 20px <-------it's not working
    onChange={this.handleEmailChange.bind(this)}
    onBlur={this.onBlur.bind(this)}/>

here is your style这是你的风格

const style = StyleSheet.create({
    email: {
        borderColor: 'white',
        width: '100%',
        borderWidth: 1,
        borderRadius: 10,
        padding: 10,
        height: 55,
        width: 378,
        marginLeft: 8.27,
        marginTop: 5,
        fontSize:15,  <------------add this line it's working 
        backgroundColor: 'white'
    },

The font size of the placeholder is same as the font size of text field.占位符的字体大小与文本字段的字体大小相同。 This means you could specify the placeholder font size inside the style prop as normal.这意味着您可以像往常一样在 style 道具中指定占位符字体大小。

<TextField placeholder="email" style={{ fontSize: 20 }} />

If you need to customise the font size of the placeholder and value differently.如果您需要自定义占位符的字体大小和不同的值。 You could use is check if there is a input value exists and apply style accordingly.您可以使用 is 检查是否存在输入值并相应地应用样式。

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

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