How to place a text over image in react native?

67,527

Solution 1

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>

Solution 2

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>

Solution 3

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.

Solution 4

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;
Share:
67,527

Related videos on Youtube

Linu Sherin
Author by

Linu Sherin

React Native Developer

Updated on July 09, 2022

Comments

  • Linu Sherin
    Linu Sherin almost 2 years

    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

  • garykwwong
    garykwwong almost 4 years
    This is more convenient way to achieve that.
  • prisar
    prisar over 3 years
    the question is about react native