【问题标题】:how to change font size in textInput text when more then 6 char entered in react native?当在反应本机中输入超过 6 个字符时,如何更改 textInput 文本中的字体大小?
【发布时间】:2018-03-07 17:13:06
【问题描述】:

我只想在 charLength 大于 6 时更改我们在 textInput 中输入的文本的字体大小。

实际字体大小为 80px,更改时应为 40 或更小

提前致谢

【问题讨论】:

    标签: react-native font-size textinput


    【解决方案1】:

    您可以为TextInput 组件赋予条件样式。

    示例

    _onChangeText(text) {
      this.setState({ fontSize: text.length > 6 ? 40 : 80 });
    }
    
    render() {
      return (
        // Giving an array of objects to style property can help you to define a -- default value
        <TextInput 
          onChangeText={this._onChangeText.bind(this)}
          style={[ {fontSize: 80}, {fontSize: this.state.fontSize} ]}
        />
      )
    }
    

    【讨论】:

    • 它显示 null 不是一个对象(评估 'this.state.fontSize')
    • 你需要在你的构造方法中给出状态的初始值。
    • 文本的长度应该从 0 开始?构造函数(道具){ 超级(道具); this.state = { count: 0, } } 像这样? @bennygenel
    • 我不明白你的意思。请使用您当前代码的minimal reproducible example 更新您的问题
    • 在我的示例中,我没有设置count。我正在设置fontSize
    猜你喜欢
    • 2020-09-22
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2013-06-18
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多