Is there any way to get the Y position of the TextInput cursor in react native, if possible without any native (android/ios) hacks? I don't mean the start and end of the cursor selection state. What I am trying to achieve is, when the keyboard is open, I would like to display a modal based on the position of the cursor, just like mentioning in Facebook and Instagram. Or may be at which line the cursor is at? That way I may be able to calculate the position of the cursor.
Meaning, if the cursor is in the beginning of the TextInput (Top of the screen) I would like to display the modal below the cursor upto the keyboard. And if the cursor is in the middle or bottom of the TextInput (middle of the visible area between the Top of the screen and Keyboard), I would like to display the modal above the cursor.
Use case:
And since, there is space between the cursor position and keyboard, I would like to display a modal in between the cursor and keyboard.
And since in this case, there is much space above the cursor to the top of the screen, I would like to display the modal above the cursor.
In order to get the position of the cursor the only thing you need is onSelectionChange https://reactnative.dev/docs/textinput#onselectionchange. The function will return the start and the end values.
If you ever had a specific case where you had to retrieve the position of a caret (your cursor's position) inside an HTML input field, you can do so using the selectionStart property of an input's value. Keep in mind that selectionStart can only be retrieved from the following list of input types: text. password.
Or, adding the below code after [self. window makeKeyAndVisible]; , you can change the tint color of TextInput/UITextField. [[UITextField appearance] setTintColor:[UIColor redColor]]; Nothing happens when you change the UITextView's tint color.
I was hoping to get the cursor's position with @JohnKrakov method, by implementing a dummy Text component inside a TextInput component. But this is not possible, by the nature of the Text component. I have managed to work it out in a react web app, but not with react native.
However, I found a medium post on how to extract a cursor’s position in react native. It is a very lengthy post, so I cannot answer the steps in this post, and instead ask you to go through his post and appreciate him for his work.
Update
There is a feature request to get the cursor position in react native. If you also need it, kindly vote the request in this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With