I am using React Native and I would like to change the cursor color of a text input. I actually got the default blue color.
How can I set a global color either in JavaScript or in AppDelegate ?
I am using React Native and I would like to change the cursor color of a text input. I actually got the default blue color.
How can I set a global color either in JavaScript or in AppDelegate ?
There is actually a prop doing this for TextInput : selectionColor
<TextInput
selectionColor={'green'}
/>
Here is the documentation.
Best way to accomplish this, if you want consistence trough the app is putting the bellow code in your root file (index.js)
import { TextInput } from 'react-native'
TextInput.defaultProps.selectionColor = 'white'
/*class....*/
Many here suggested using selectionColor
:
import {TextInput} from 'react-native';
TextInput.defaultProps.selectionColor = 'red';
As of RN 0.63, this solution is still inefficient for at least two reasons:
WebView
components will get the default cursor color on both platforms.Therefore, the right way to change the cursor color is by editing the native settings instead.
Go to android/app/src/main/res/values/styles.xml
and add the following lines to the custom section:
<item name="android:colorControlActivated">#FF0000</item>
<item name="android:textColorHighlight">#FF9999</item>
Go to ios/MyApp/AppDelegate.m
and before [self.window makeKeyAndVisible];
add:
self.window.tintColor = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1];
Finally, rebuild the apps to see the result of your edits.
You can just change the cursor color by changing the selection color according to the documentation as shown bellow,
<Input
...
selectionColor={"black"}
/>
Yes, we can do it by setting tint color.
In AppDelegate.m
of project.
Adding the below code between self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
and [self.window makeKeyAndVisible];
, you can change the global tint color.
self.window.tintColor = [UIColor redColor]; // Here is your color.
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.
And I couldn't find a way to implement it with a style of JaveScript.
For React Native 0.62 +
import {TextInput } from 'react-native'
TextInput.defaultProps = TextInput.defaultProps || {};
TextInput.defaultProps.selectionColor = 'transparent';
Add these line before Main function call in App.js
Eg :-
..Other code
TextInput.defaultProps = TextInput.defaultProps || {};
TextInput.defaultProps.selectionColor = 'transparent';
const App = () => {
...Other code