【问题标题】:how to change placeholder textSize in react-native如何在 react-native 中更改占位符 textSize
【发布时间】:2026-01-03 13:35:01
【问题描述】:

在我的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)}/>

当我写这样的颜色对我有用但大小不起作用时,谁能给我建议如何更改占位符文本的大小,非常感谢任何帮助

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    我不确定你是否只能指定 placeholderText 的大小。但是您可以从 TextInput 更改输入文本的 fontSize:

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

    也许您的解决方案可以在没有 px 的情况下工作?!但我不这么认为。如果您想拥有与 InputText 不同的 PlaceholderText,那么您可以访问 TextInput 的 onChangeText 方法并在您在 Textfield 中键入内容后立即更改 fontSize。

    【讨论】:

    • 这是正确答案。您可以通过 Stylesheet 对象更改 fontSize 来修改占位符的文本大小。但是没有像placeholderTextSize 这样的属性。它只适用于颜色。
    【解决方案2】:

    TextInput 组件可以这样创建。

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

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请编辑您的答案以添加解释并说明适用的限制和假设。
    【解决方案3】:

    没有办法(据我记得)直接更改占位符的字体大小,因为当您使用样式更改 fontSize 时,您同时更改了输入 fontSize 和占位符 fontSize。不过,有办法……

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

    然后使用状态检查输入字段是否为空。如果为空,则将fontSize改为:

    { fontSize: text ? 18 : 12 }
    

    但是,重要的是要注意,即使这种方法有效,但由重新渲染引起的延迟当然是值得注意的。

    【讨论】:

      【解决方案4】:
      <TextInput 
      style={{fontSize:12}} 
      placeholder='any text' 
      onChangeText={(input)=>this.setState({input: input})}
      placeholderTextColor='green'/>
      

      【讨论】:

      • 请解释你的答案。
      • 当你增加 fontSize 占位符大小增加
      最近更新 更多