【问题标题】:numberOfLines TextInput property not workingnumberOfLines TextInput 属性不起作用
【发布时间】:2016-06-26 12:03:59
【问题描述】:

我在react-native 中创建了一个应用程序,并且我可以选择在消息中聊天。当我在 TextInput 内部单击并键入两行时,上面的行被隐藏了。为了解决这个问题,我在文档中看到了 numberOfLines 属性,但它不起作用。

这是我的代码:

<TextInput

                ref='textInput'
                multiline={true}
                numberOfLines: {5}
                onChangeText={this.onChangeText}
                style={[styles.textInput, {height: context.props.textInputHeight}]}
                placeholder={context.props.placeholder}
                placeholderTextColor="#5A5A5A"
                value={context.state.text}/>

我也在getDefaultProps函数中尝试过:

getDefaultProps() {
    return {
      placeholder: 'Type a message...',
      navHeight: 70,
      textInputHeight: 44,
      numberOfLines:5,
      maxHeight: Screen.height,
    };
  },

但是没有用。

【问题讨论】:

    标签: javascript android reactjs react-native textinput


    【解决方案1】:

    继续:

    <TextInput
      ...
      numberOfLines={Platform.OS === 'ios' ? null : numberOfLines}
      minHeight={(Platform.OS === 'ios' && numberOfLines) ? (20 * numberOfLines) : null}
    />
    

    【讨论】:

    • 数字 20 究竟代表什么?
    • @kojow7 每行的高度
    • 每行硬编码 20 是绝对不正确的。在 Android 上 numberOfLines 会自动计算每行需要多少高度,因此 textinput 看起来很完美
    【解决方案2】:

    您可以使用maxHeight 和minHeight 来接受您想要的。对于标准文本字体大小,提供maxHeight={60} 将使 TextInput 在 3 行后可滚动。这对 IOS 有好处 - 对于 Android,您可以使用 numberOfLines 属性。

    【讨论】:

    • 是的,现在我们可以使用 maxHeight 属性,但两年多前我问这个问题时,文档中没有它。感谢您的更新!
    【解决方案3】:

    你有numberOfLines: {5},应该是numberOfLines={5}。或者这只是 SO 中的一个错字?

    另外,textAlignVertical: 'top'的造型也很推荐。

    【讨论】:

    • 当我删除样式style={[styles.textInput, {height: context.props.textInputHeight}]} 中的高度并且如果我添加textAlignVertical: 'top' 它不会显示我在文本框中写入的文本
    【解决方案4】:

    你应该设置maxHeight。我确实设置了我的maxHeight={70}。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多