Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change image and text color when clicking using react-native?

I am using TouchableHighlight, but I can change only background color using underlayColor. But how to change other content?

like image 944
kyrilkin Avatar asked Dec 04 '15 12:12

kyrilkin


People also ask

How do I change the color of an image in React Native?

Well, you can also change the color of an image or icon in React Native. Precisely saying, you can change the color of all the non-transparent pixels of the image. To achieve this, all you need is to add the style prop tintColor to your image component.

How do I change the text color of text input in React Native?

To change the text color of text input in React Native, we can set the color style to the text color. to set the color style to 'green' on the TextInput . Therefore, we see that the color of the text we typed into the TextInput is green.

How do you make a photo black and white in React Native?

To convert an image to grayscale in React Native, we can add the same image twice, with one set to a different opacity than the other one, and add the tintColor prop. to set the tintColor to 'gray' to add the grayscale tint. Then we add the same image with the position set to 'absolute' to put it over the gray image.


3 Answers

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight
} = React;

var  colors = ['#ddd', '#efefef', 'red', '#666', 'rgba(0,0,0,.1)', '#ededed'];
var backgroundcolors = ['green', 'black', 'orange', 'blue', 'purple', 'pink'];

var SampleApp = React.createClass({

  getInitialState() {
    return {
        color: 'orange',
      backgroundColor: 'rgba(0,0,0,.1)'
    }
  },

  _changeStyle() {
    var color = colors[Math.floor(Math.random()*colors.length)];
    var backgroundColor = backgroundcolors[Math.floor(Math.random()*backgroundcolors.length)];
    this.setState({
        color: color,
      backgroundColor: backgroundColor
    })
  },

  render: function() {
    return (
      <View style={styles.container}>
        <TouchableHighlight 
        onPress={ () => this._changeStyle() }
        style={{ backgroundColor: this.state.backgroundColor, height: 60, flexDirection: 'row', alignItems:'center', justifyContent: 'center' }}>
                <Text style={{ fontSize: 22, color: this.state.color }}>CHANGE COLOR</Text>
        </TouchableHighlight>

        <TouchableHighlight style={{ backgroundColor: 'red', height: 60, flexDirection: 'row', alignItems:'center', justifyContent: 'center' }}>
          <Text style={{ color: 'white', fontSize:22 }} >Click Me</Text>
        </TouchableHighlight>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop:60
  }
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);
like image 130
Nader Dabit Avatar answered Oct 05 '22 18:10

Nader Dabit


This answer is assuming you want to change the color just while the button is depressed:

Use TouchableWithoutFeedback and define your own onPressIn and onPressOut functions to change the text color.

<TouchableWithoutFeedback onPressIn={this.colorText}  onPressOut={this.resetText}> 
  <Text style={[styles.textColored()]}>MyText</Text>
</TouchableWithoutFeedback>

colorText: function() {
  this.setState({textColored: true});
},
resetText: function() {
  this.setState({textColored: false});
},
textColored: function() {
  if(this.state.textColored) {
    return styles.textColored;
  } else {
    return styles.textNormal;
  }
}
like image 21
Eadz Avatar answered Oct 05 '22 18:10

Eadz


With TouchableHighlight you can do it like this

state = { selected: false };

setSelected(selected: boolean) {
    this.setState({ selected: selected });
}

textStyle() {
    return this.state.selected ? styles.textSelected : styles.text;
}

And then in the render function

<TouchableHighlight
    underlayColor={Theme.palette.accent}
    onPress={() => onPress()}
    onShowUnderlay={() => this.setSelected(true)}
    onHideUnderlay={() => this.setSelected(false)}
>
    <Text style={this.textStyle()}>{text}</Text>
</TouchableHighlight>
like image 41
box Avatar answered Oct 05 '22 19:10

box