0

I've taken one example here which is similar to my requirement. So suppose I have two textinput, while typing on textinput 2 some part of that is appending on textinput 1's value also. so in Android whenever a new part is added on value it always shows me the end of the value in textinput while in iOS it shows me '...' and displays the start of the value. But I want to show always end part of the value in textinput start display as '...' is fine. So to sum it up I want the same behaviour in ios as in android

import React from 'react';
import {StyleSheet, TextInput,Button} from 'react-native';
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';

const TextInputExample = () => {
  const [text, onChangeText] = React.useState('Useless Text');
  const [number, onChangeNumber] = React.useState('');

  return (
   
      <SafeAreaView style={{flex:1,}}>
        <TextInput
          style={styles.input}
          onChangeText={onChangeText}
          value={text}
        />
        <TextInput
          style={styles.input}
          onChangeText={onChangeText}
          placeholder="useless placeholder"
          keyboardType="numeric"
        />
        <Button title={'touch'}/>
      </SafeAreaView>
  
  );
};

const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
    padding: 10,

  },
});

export default TextInputExample;

ios current behaviour enter image description here

Android current behaviour:

enter image description here

Actual requirement SS enter image description here

While selecting any emoji from the emoji picker it is appending on text input value state, so it should display users added emoji at end without focusing input.

3 Answers 3

0

This can be controlled on iOS since the RN release which includes the fix(ios): line break mode property added for iOS PR. There is basically a new prop now called lineBreakStrategyIOS. If you set it to push-out, then you have achieved the desired bevahior.

<TextInput
   style={styles.input}
   onChangeText={onChangeText}
   value={text}
   lineBreakStrategyIOS="push-out"
/>

If you are using an older RN version, then you could use a workaround by using textAlign='left':

<TextInput
   style={styles.input}
   onChangeText={onChangeText}
   value={text}
   textAlign="left"
/>
Sign up to request clarification or add additional context in comments.

1 Comment

I've react-native 0.65.3 so the first solution can't be used. and the second one just removing ... at the end but not show the end part of the text in text input. See I've uploaded the actual implementation ss, I'm appending emoji in value state of textinput.
0

i think this will help you get the '...' at the end of screen when you changed the focus form textinput field in android.

    import React, { useState, useRef, useEffect } from "react";
import { TextInput, View, Text, TouchableOpacity, Keyboard } from "react-native";

const App = () => {
  const [text, setText] = useState("");
  const [isFocused, setIsFocused] = useState(false);
  const inputRef = useRef(null);

  useEffect(() => {
    if (isFocused) {
      setTimeout(() => inputRef.current?.focus(), 50);
    }
  }, [isFocused]);

  return (
    <View style={{ padding: 20, marginTop: 200 }}>
      {!isFocused && text.length > 0 ? (
        <TouchableOpacity onPress={() => setIsFocused(true)} activeOpacity={1}>
          <Text
            numberOfLines={1}
            ellipsizeMode="tail"
            style={{
              borderWidth: 1,
              padding: 10,
              height: 40,
            }}
          >
            {text}
          </Text>
        </TouchableOpacity>
      ) : (
        <TextInput
          ref={inputRef}
          value={text}
          onChangeText={setText}
          onFocus={() => setIsFocused(true)}
          onBlur={() => {
            // Prevent blur if input is still focused
            setTimeout(() => {
              if (inputRef.current?.isFocused()) return;
              setIsFocused(false);
            }, 100);
          }}
          style={{
            borderWidth: 1,
            padding: 10,
            height: 40,
          }}
        />
      )}
    </View>
  );
};

export default App;

2 Comments

I want to add the same behaviour in iOS as Android.
Remove ... and show always last part of text in input
0

I have already tried with textInputRef.current.setNativeProps({ selection: { start: text.length, end: text.length },});, but was not working in ios.

Then I found that iOS requires focus before selection. So finally I resolved by adding calling below function in onchange or on any other event you want.

constmoveCursorToEnd = () => {
if (textInputRef.current) {
setIsEditable(false); // Disable input to prevent keyboard from openingsetTimeout(() => {
        textInputRef.current.focus(); // iOS requires focus before selection changesetTimeout(() => {
          textInputRef.current.setNativeProps({
selection: { start: text.length, end: text.length },
          });
          textInputRef.current.blur(); // Blur immediately after setting selectionsetIsEditable(true); // Re-enable input
        }, 10);
      }, 10);
    }

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.