简体   繁体   English

iOS TextInput 高度相对于字体大小 react-native

[英]iOS TextInput height relative to font size react-native

I'm having trouble with the TextInput module for React-Native我在使用 React-Native 的TextInput模块时遇到问题

https://facebook.github.io/react-native/docs/textinput.html https://facebook.github.io/react-native/docs/textinput.html

On Android it's nice and simple, you create the TextInput give it a font-size and it sets the height of the TextInput control accordingly.在 Android 上,它很好也很简单,你创建 TextInput 给它一个字体大小,它相应地设置 TextInput 控件的高度。

On the other hand, with iOS unless you provide an explicit height for the TextInput in the style it will be 0.另一方面,对于 iOS,除非您为样式中的 TextInput 提供明确的高度,否则它将为 0。

This would be okay normally, but because I use variable font sizes in my app.这通常没问题,但因为我在我的应用程序中使用可变字体大小。 I need the TextInput height to match the font-size.我需要 TextInput 高度来匹配字体大小。

I did try setting the text-input height to the same value as the font-size.我确实尝试将文本输入高度设置为与字体大小相同的值。 That didn't work correctly and I don't think the two are relative.那没有正常工作,我不认为两者是相对的。

I have some multiline text inputs and I would like to calculate the height I want for them based on the height for a single line.我有一些多行文本输入,我想根据单行的高度计算我想要的高度。

pseudo code伪代码

lineHeight = getHeightForFontSize(15);
multiLineHeight = lineHeight * 4; // 4 lines

So in summary.所以总结一下。 How do I get the height for a TextInput view based on the font-size (known in advance).如何根据字体大小(预先知道)获取 TextInput 视图的高度。

You can use onContentSizeChange for getting font height on multiline textinput.您可以使用onContentSizeChange获取多行文本输入的字体高度。 I'm using this for changing height of textinput after getting too long text.在获得太长的文本后,我正在使用它来更改文本输入的高度。 But be sure you are using multiline TextInput.但请确保您使用的是多行 TextInput。 Otherwise, it will not work.否则,它将无法工作。

<TextInput
  style={{height: this.state.height, borderColor: 'gray', borderWidth: 1}}
  onChangeText={::this.onMessageChange}
  value={this.props.message}
  multiline
  onContentSizeChange={({ nativeEvent: { contentSize: { width: txtWidth, height: txtHeight } } }) => {
    if (txtHeight > 40) {
      this.setState({
         height: txtHeight,
      });
    }
  }}
/>

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

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