简体   繁体   English

使 React Native Text 组件在 Android 上的 TextInput 内不占用空间

[英]Make React Native Text component take no space inside TextInput on Android

I have a Text element inside a TextInput that I want to make take no space.我在TextInput中有一个Text元素,我想让它不占用空间。 Here's a preview of the component:这是组件的预览:

export function Component() {
  function renderText() {
    return (
      <Text>
        This is normal text
        <Text style={styles.hiddenText}>This very long text should be hidden because it is metadata.</Text>
      </Text>
    );
  }

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        multiline={true}
      >
        {renderText()}
      </TextInput>
    </View>
  );
}

Here are the styles that I have applied to the Text element that I want to hide:这是我已应用于要隐藏的Text元素的 styles:

hiddenText: {
  fontSize: 0.000001,
  backgroundColor: 'red' // just to show that it actually appears on Android
}

These styles work as expected on iOS, but on Android the supposedly hidden Text element still occupies quite a lot of space ( see Snack demo ).这些 styles 在 iOS 上按预期工作,但在 Android 上,本应隐藏的Text元素仍然占据相当大的空间(参见 Snack 演示)。

See iOS screenshot见 iOS 截图

See Android screenshot见 Android 截图

In case you are wondering why I want to do this, the above is a simplified version of my real app.如果您想知道我为什么要这样做,上面是我真实应用程序的简化版本。 In reality, I have a text input that allows for adding comments and the users can also tag each other.实际上,我有一个文本输入,允许添加评论,用户也可以互相标记。 My current implementation keeps the tagged user inside the text itself in the following format @user's full name|userId;我当前的实现将标记的用户保留在文本本身中,格式如下@user's full name|userId; because I found this approach to be the easiest in terms of manipulating the actual content of the text input.因为我发现这种方法在操作文本输入的实际内容方面是最简单的。 I have to keep the id of the user along with the full name as the id is what is actually sent to the API.我必须保留用户的 id 和全名,因为 id 是实际发送到 API 的内容。

How can I hide the red background text on Android and make it look the same as on iOS?如何隐藏 Android 上的红色背景文本并使其看起来与 iOS 上的相同?

I've done some amendments in your css, please have a look into below snack我对你的 css 做了一些修改,请看下面的小吃

https://snack.expo.io/KRMyR7wYq https://snack.expo.io/KRMyR7wYq

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

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