Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Three dots when text in TextInput is longer than TextInput and when not in focus

So I'll try to explain my scenario.

I have a singleline TextInput. When the user is onFocus, the text within the TextInput can be longer than the TextInput itself.

However, when the user is no longer onFocus, Android and iOS behave differently.

iOS: Three dots are added to the end of the line and the beginning of the text is shown.

enter image description here

Android: No changes are made.

Behaviour in Android

How can I make the TextInput in Android behave as it does in iOS for this particular scenario?

Thank you

like image 787
Amaros Avatar asked Jul 03 '17 07:07

Amaros


People also ask

What is the default font size in React Native?

font-family: 'lucida grande', tahoma, verdana, arial, sans-serif; font-size: 11px; color: #141823; } All elements in the document will inherit this font unless they or one of their parents specifies a new rule. In React Native, we are more strict about it: you must wrap all the text nodes inside of a <Text> component.

How to add ellipses to text in React Native?

To Achieve ellipses for the text use the Text property numberofLines= {1} which will automatically truncate the text with an ellipsis you can specify the ellipsizeMode as "head", "middle", "tail" or "clip" By default it is tail If you want read more behavior, then you can use the react-native-read-more-text library:

Is it possible to call focus() on a text input node?

As of React Native 0.36, calling focus () (as suggested in several other answers) on a text input node isn't supported any more. Instead, you can use the TextInputState module from React Native. I created the following helper module to make this easier:

Can you have a text node under a view in React Native?

In React Native, we are more strict about it: you must wrap all the text nodes inside of a <Text> component. You cannot have a text node directly under a <View>. You also lose the ability to set up a default font for an entire subtree.


1 Answers

 const placeHolderLengthLimit = useMemo(
        () => (tooltip ? 31 : 37),
        [tooltip],
    );

    const placeholderString = useMemo(
        () =>
            placeholder
                ? Platform.OS === 'ios' ||
                  placeholder?.length <= placeHolderLengthLimit
                    ? placeholder
                    : placeholder?.slice(0, placeHolderLengthLimit) + '...'
                : undefined,
        [placeHolderLengthLimit, placeholder],
    );

                   <TextInput
                        placeholder={placeholderString}
                    />
like image 80
Dmitry Preeternal Avatar answered Nov 14 '22 16:11

Dmitry Preeternal