Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Auto width View with <UITextView> inside #17

Open
mk-nickyang opened this issue Oct 8, 2024 · 3 comments
Open

[Question] Auto width View with <UITextView> inside #17

mk-nickyang opened this issue Oct 8, 2024 · 3 comments

Comments

@mk-nickyang
Copy link

Hi there, just wondering how to do an auto-width View with <UITextView> inside?

For example in the code below, the one with <UITextView> will take up the whole width, while the one with RN <Text> will render as inline text

import { Text, View } from "react-native";
import { UITextView } from "react-native-uitextview";

export default function HomeScreen() {
  return (
    <View
      style={{
        flex: 1,
        alignItems: "center",
        justifyContent: "center",
        gap: 8,
      }}
    >
      <View style={{ backgroundColor: "blue" }}>
        <UITextView selectable uiTextView style={{ color: "white" }}>
          UITextView - Hello World
        </UITextView>
      </View>

      <View style={{ backgroundColor: "blue" }}>
        <Text style={{ color: "white" }}>Text - Hello World</Text>
      </View>
    </View>
  );
}



I'm using

"expo": "~51.0.28",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.74.5",
"react-native-uitextview": "^1.4.0"

Awesome library btw, appreciate the great work 🙏

@antoniogoulao
Copy link

antoniogoulao commented Nov 14, 2024

+1
It looks like an issue still to be solved since on the 🦋 app, for messages they use a workaround to copy text.

@yanoyuki
Copy link

+1

@yanoyuki
Copy link

I created the following component, assuming that children will not change after the initial render.

This component ensures that the width of <UITextView> matches the dimensions of a placeholder <Text> element measured during the initial render. In my case, this approach worked without any issues.

import { type ComponentProps, memo, useState } from "react";
import { type LayoutChangeEvent, Text } from "react-native";
import { UITextView } from "react-native-uitextview";

const SelectableText = memo(({ children, style, ...props }: ComponentProps<typeof UITextView>) => {
  const [measuredWidth, setMeasuredWidth] = useState<number | null>(null);

  const handleLayout = (e: LayoutChangeEvent) => {
    const { width } = e.nativeEvent.layout;
    setMeasuredWidth(width);
  };

  if (measuredWidth === null) {
    return (
      <Text {...props} style={[style, { opacity: 0 }]} onLayout={handleLayout} selectable>
        {children}
      </Text>
    );
  }

  return (
    <UITextView {...props} style={[style, { width: measuredWidth, maxWidth: measuredWidth }]} selectable uiTextView>
      {children}
    </UITextView>
  );
});

export default SelectableText;

To improve the user experience, adding opacity: 0 to <Text> until <UITextView> is rendered might be helpful. This prevents users from briefly seeing the placeholder content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants