Anu
Anu

Reputation: 1742

How to place a text over image in react native?

How to vertically place a text over image in react native? I found this doc. But i can't do like that , I can't add text tag as a child component of Image tag.I've tried like the below.

 <Card>
    <CardSection>
            <View style={styles.container}>
              <Image source={require('../Images/4.jpg')} style={styles.imageStyl}  />
    <Text style={styles.userStyle}>       
            {this.props.cat.name}
             </Text>
             </View>
            </CardSection>
            </Card>
const styles= StyleSheet.create({

    container:{
         flex: 1,
    alignItems: 'stretch',
    justifyContent: 'center',
    },
    imageStyl: {
    flexGrow:1,
    width:"100%",
    height:200,
    alignItems: 'center',
    justifyContent:'center',
  },
    userStyle:{
        fontSize:18,
        color:'black',
        fontWeight:'bold',
        textAlign: 'center'
    },
});

How to place the text to the center of image?Getting something like this image

Upvotes: 29

Views: 94949

Answers (5)

Chandrahas Balleda
Chandrahas Balleda

Reputation: 2822

You can import ImageBackground instead of Image from react-native and pass the text as children to the ImageBackground. This does the magic. Please see the code below:

<View style={styles.imageWrapper}>
     <ImageBackground style={styles.theImage} source={{uri : item.imageUrl}}>
          <Text>Hey</Text>
     </ImageBackground>
</View>

const styles = StyleSheet.create({
    imageWrapper: {
        height: 200,
        width: 200,
        overflow : "hidden"
    },
    theImage: {
        width: "100%",
        height: "100%",
        resizeMode: "cover",
    }
})

We can use positioning too as stated by many, but I prefer using ImageBackground.

Upvotes: 18

jaideep rawat
jaideep rawat

Reputation: 471

use this:

<ImageBackground source={require('background image path')} style={{width: '100%', height: '100%'}}>
   <View style={{position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, justifyContent: 'center', alignItems: 'center'}}>
     <Text>Centered text</Text>
   </View>
</ImageBackground>

Upvotes: 47

Klaudia Brysiewicz
Klaudia Brysiewicz

Reputation: 129

I have my own component to do so:

import React from 'react';
import { View, Image } from 'react-native';

const BackgroundImage = (props) => {

  const { container, image } = styles;


  return (

    <View style={container}>
      <Image
      style={[image, 
        { resizeMode: props.resizeMode,    
        opacity: props.opacity}
      ]}  
      source={props.source}***strong text***
      />
    </View>

  )
};

const styles = {
  container: {
    position: 'absolute',
    top: 0,
    left: 0,   
    width: '100%',
    height: '100%',
  },
  image: {  
    flex: 1,  
  }
};

export {BackgroundImage};

that component will fill your container with any image you wish ;)

import React from 'react';
import { View, Image } from 'react-native';

class List extends Component {
   render() {
    let source = {uri: 'http://via.placeholder.com/350x150'};
    return (
           <View style = {{backgroundColor: 'black'}>
              <BackgroundImage
               resizeMode="cover"
               opacity={0.6}
               source={source}
               />
               <Text>Hello World</Text>
            </View>
     )
   }
   export default List;

Upvotes: 4

Nazmul Tanvir
Nazmul Tanvir

Reputation: 57

Its so similar like normal HTML and CSS place a text over image. You have to place a text over image by making text's absolute.

  • modified those code as it is :

    userStyle:{
        position : absolute;
        bottom : 0,
        top : 50,
        left : 0,
        right : 0,
        alignItems: 'center',
        justifyContent:'center',
    }
    

I hope those code will fix your problem.

Upvotes: 1

Clad Clad
Clad Clad

Reputation: 2771

You have to use in the "css" position:'absolute' And then place your text using the css properties (like top, bottom, right, left)


React Native absolute positioning horizontal centre

Wrap the child you want centered in a View and make the View absolute.

<View style={{position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, justifyContent: 'center', alignItems: 'center'}}> <Text>Centered text</Text> </View>

Upvotes: 52

Related Questions