Reputation: 1005
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 ?
Upvotes: 70
Views: 71645
Reputation: 11
selectionColor="red"
by using above code, you'll get red color of "|" in search box/text input.
For example :
<TextInput style={styles.searchInput}placeholder="Search Vehicle" placeholderTextColor="#868687" value={search} selectionColor="red" onChangeText={setSearch}/>
Upvotes: 1
Reputation: 119
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
Upvotes: 0
Reputation: 2613
There is actually a prop doing this for TextInput : selectionColor
<TextInput
selectionColor={'green'}
/>
Here is the documentation.
Upvotes: 240
Reputation: 9321
You can just change the cursor color by changing the selection color according to the documentation as shown bellow,
<Input
...
selectionColor={"black"}
/>
Upvotes: 10
Reputation: 751
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.
Upvotes: 17
Reputation: 455
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....*/
Upvotes: 27
Reputation: 1918
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.
Upvotes: 5