Home > Blockchain >  How can i add border radius to my image fron each side without effecting the text?
How can i add border radius to my image fron each side without effecting the text?

Time:01-25

How can I add a border radius to my image from each side without affecting the username text? I want to make the image feel good by adding a border radius from each side like yt shorts so what I did I gave it borderTopLeftRadius borderTopRightRadius 30 each same with the bottom but then it makes the username text looks bad can you help me with that? I am using react native

import React from 'react';
import { StyleSheet, Text, View, Image, ActivityIndicator } from 'react-native';
import { useState } from 'react';

const PostCard = ({ username, profile_image, postImage }) => {
  const [isLoading, setIsLoading] = useState(true);
  return (
    <View style={styles.container}>
      <View style={styles.c1}>
        <Image
          source={{ uri: profile_image }} 
          style={styles.profilepic} 
          onl oadEnd={() => setIsLoading(false)}
        />
        {isLoading && <ActivityIndicator style={styles.spinner} />}
        <Text style={styles.username}>{username}</Text>
      </View>
      <Image
        source={{ uri: postImage }}
        style={styles.image} // this is the image that i want to style
        onl oadEnd={() => setIsLoading(false)}
      />
      {isLoading && <ActivityIndicator style={styles.spinner} />}
    </View>
  );
};


export default PostCard

  const styles = StyleSheet.create({
    container: {
      backgroundColor: 'white',
      width: '100%',
      // height: 350,
      borderRadius: 10,
      marginVertical: 10,
      overflow: 'hidden',
      borderColor: 'white',
      borderWidth: 1,
    },
    c1: {
      width: '100%',
      flexDirection: 'row',
      alignItems: 'center',
      padding: 10,
      backgroundColor: 'black',
    },
    profilepic: {
      width: 30,
      height: 30,
      borderRadius: 30,
      borderColor: 'white',
      borderWidth: 1,
    },
    username: {
      color: 'white',
      marginLeft: 10,
      fontSize: 17,
      fontWeight: 'bold',
    },
    image: {
      width: '130%',
      aspectRatio: 1,
    }
  })

CodePudding user response:

One way to add a border radius to the image while keeping the username text unaffected is to wrap the image in a separate View component and apply the border radius styles to that component. Here's an example of how you can do that:

import React from 'react';
import { StyleSheet, Text, View, Image, ActivityIndicator } from 'react- native';
import { useState } from 'react';

const PostCard = ({ username, profile_image, postImage }) => {
  const [isLoading, setIsLoading] = useState(true);
  return (
<View style={styles.container}>
  <View style={styles.c1}>
    <Image
      source={{ uri: profile_image }} 
      style={styles.profilepic} 
      onl oadEnd={() => setIsLoading(false)}
    />
    {isLoading && <ActivityIndicator style={styles.spinner} />}
    <Text style={styles.username}>{username}</Text>
  </View>
  <View style={styles.imageContainer}>
    <Image
      source={{ uri: postImage }}
      style={styles.image} // this is the image that i want to style
      onl oadEnd={() => setIsLoading(false)}
    />
    {isLoading && <ActivityIndicator style={styles.spinner} />}
  </View>
</View>
 );
};


export default PostCard

   const styles = StyleSheet.create({
container: {
  backgroundColor: 'white',
  width: '100%',
  // height: 350,
  borderRadius: 10,
  marginVertical: 10,
  overflow: 'hidden',
  borderColor: 'white',
  borderWidth: 1,
},
c1: {
  width: '100%',
  flexDirection: 'row',
  alignItems: 'center',
  padding: 10,
  backgroundColor: 'black',
},
profilepic: {
  width: 30,
  height: 30,
  borderRadius: 30,
  borderColor: 'white',
  borderWidth: 1,
},
username: {
  color: 'white',
  marginLeft: 10,
  fontSize: 17,
  fontWeight: 'bold',
},
imageContainer: {
  borderTopLeftRadius: 30,
  borderTopRightRadius: 30,
  borderBottomLeftRadius: 30,
  borderBottomRightRadius: 30,
  overflow: 'hidden',
},
image: {
  width: '130%',
  aspectRatio: 1,
}
 })

In this example, I wrapped the Image component inside a View component and applied the border radius styles to the View component instead of the Image component. The View component is what gives the image its rounded corners, while the Text component (username) is not affected by the border radius styles.

CodePudding user response:

You can add a separate container view around the image and apply the border radius to that container view. Then, you can position the image and username text inside of that container view so that they are not affected by the border radius. Here's an example of how you could modify your code:

<View style={styles.container}>
  <View style={styles.c1}>
    <Image
      source={{ uri: profile_image }} 
      style={styles.profilepic} 
      onl oadEnd={() => setIsLoading(false)}
    />
    {isLoading && <ActivityIndicator style={styles.spinner} />}
    <Text style={styles.username}>{username}</Text>
  </View>
  <View style={styles.imageContainer}>
    <Image
      source={{ uri: postImage }}
      style={styles.image}
      onl oadEnd={() => setIsLoading(false)}
    />
    {isLoading && <ActivityIndicator style={styles.spinner} />}
  </View>
</View>

And in your styles object:

imageContainer: {
  width: '130%',
  aspectRatio: 1,
  borderTopLeftRadius: 30,
  borderTopRightRadius: 30,
  borderBottomLeftRadius: 30,
  borderBottomRightRadius: 30,
  overflow: 'hidden',
}

This way, the border radius will be applied to the image container and your username text will remain unaffected.

  • Related