React-Native Image Opacity giving opacity to text - react-native

Im using an image as basically a view and putting text inside of it, i tried to give opacity to the image but everytime i try the text gets opacity too I don't really know why it happens
<View style={{flex: 1}}>
<View style={{height: 180,}}>
<Image source={{uri : this.state.fullURLImage}} style={{flex: 1,width: window.width,justifyContent:'center',alignItems:'center',opacity: 0.7}}>
<Text style={{textAlign: 'center',fontWeight: 'bold',color:'white',fontSize:16,}}>{this.state.title}</Text>
<Text style={{color:'white',fontSize:14,}}>{'\n'}July {this._try(this.state.day)} | {this.state.time_start} - {this.state.time_end}</Text>
</Image>
</View>
<View style={{flexGrow: 1, backgroundColor: 'white',}}>
<Text style={{textAlign:'center',color:'#1B3D6C',fontWeight:'bold',margin:10,marginTop: 40}}>{this.state.author}</Text>
<Text style={{margin:10,textAlign: (Platform.OS === 'ios') ? 'justify' : 'left'}}>{this.state.text}</Text>
</View>
<Image
source={{uri : this.state.fullURLAuthor}}
style={{
position: 'absolute',
top: 180 - 64 / 2,
height: 64,
width: 64,
left: (Dimensions.get('window').width - 64) / 2,
borderRadius: 32,
}}
/>
</View>

You need to put Text outside of image tag and give them position
OR
Also give opacity using like {backgroundColor: 'rgba(0,0,0,0.5)'}}

You can just use <ImageBackground> instead of <Image> and apply opacity and color as mentioned below.
<ImageBackground
style={{flex: 1,height: 110,resizeMode: 'cover',}}
source={imageSoure}
borderRadius={5}
resizeMode='cover'
imageStyle={{ opacity: 0.3,backgroundColor: 'YourFavouredColor'}}
>
<Text>
{someText}
</Text>
<Text>
{someOtherText}
</Text>
</ImageBackground>

As #Nisarg already mentioned, it's nice to position the text from outside of the image. And meanwhile here is the code for changing opacity for the background:
<Image
style={{
resizeMode: "contain",
height: 100,
width: 200,
opacity:0.1
}}
source={require("#expo/snack-static/react-native-logo.png")}
/>

React draws components in order of appearence.
That said, I think your Image is being drawn on top of Text.
You have more than one way to achieve your layout, but following your code try this:
<View style={{flex: 1}}>
<Image
source={{uri : this.state.fullURLAuthor}}
style={{
position: 'absolute',
top: 180 - 64 / 2,
height: 64,
width: 64,
left: (Dimensions.get('window').width - 64) / 2,
borderRadius: 32,
}}
/>
<View style={{height: 180,}}>
<Image source={{uri : this.state.fullURLImage}} style={{flex: 1,width: window.width,justifyContent:'center',alignItems:'center',opacity: 0.7}}>
<Text style={{textAlign: 'center',fontWeight: 'bold',color:'white',fontSize:16,}}>{this.state.title}</Text>
<Text style={{color:'white',fontSize:14,}}>{'\n'}July {this._try(this.state.day)} | {this.state.time_start} - {this.state.time_end}</Text>
</Image>
</View>
<View style={{flexGrow: 1, backgroundColor: 'white',}}>
<Text style={{textAlign:'center',color:'#1B3D6C',fontWeight:'bold',margin:10,marginTop: 40}}>{this.state.author}</Text>
<Text style={{margin:10,textAlign: (Platform.OS === 'ios') ? 'justify' : 'left'}}>{this.state.text}</Text>
</View>
</View>

Related

Create Layout with absolute Values and Flexbox in React Native

I am a iOS Developer trying to learn React Native. Currently I am trying to figure out how to create a layout like this with FlexBox:
I am not sure how to use relative values for with in react native. When I am trying to implement it I am getting this:
As you can see, the view in the middle cutting the views on the left and right at there center. Any ideas? Here's my code:
<View style={{flexDirection: 'row', flex: 1}}>
<SafeAreaView>
<TouchableOpacity style={{left: 20, top: 24, height: 44, width: 44}}>
<Image
source={require('../../assets/images/backButton.png')}
style={{height: '100%', width: '100%'}}
/>
</TouchableOpacity>
</SafeAreaView>
<View style={{flex: 2, backgroundColor: '#FF2F2F'}}></View>
<SafeAreaView>
<Text
style={{
right: 20,
top: 24,
height: 44,
fontSize: 14,
}}>
1 / 6
</Text>
</SafeAreaView>
</View>
The issue here is that you have incorrectly ordered the classes(View, SafeView, etc) which are causing the layout to overlap and preventing you from getting the desired result.
Check my solution proposed below:
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flexDirection: 'row', flex: 1, height: "100%"}}>
<TouchableOpacity style={{height: 44, width: 44, margin: 10}}>
<Image
source={require('../../assets/images/backButton.png')}
style={{height: '100%', width: '100%'}}
/>
</TouchableOpacity>
<View style={{flex: 1, backgroundColor: '#FF2F2F'}}></View>
<Text
style={{
padding: 14,
fontSize: 14,
}}>
1 / 6
</Text>
</View>
</SafeAreaView>
);
Running example for the solution on expo Snack
Feel free to explore and edit it!

Change the rectangular view into quadrilateral view through style - react native

Change the rectangular view into quadrilateral view through style
I need to have the quadrilateral background in the view. If it can be done through style, it will be great. Otherwise I've use the image as its background. I dont want to do it however since it wil increase app size as well.
<View>
<Image source={{uri: 'https://previews.123rf.com/images/jaboy/jaboy1706/jaboy170600065/79609763-new-green-light-scenery-background.jpg'}}
style={{width:200, height: 70 }} />
<View style={{ backgroundColor: 'red', padding: 10, paddingTop: 25 }}>
<Text style={styles.red}>just red</Text>
<Text style={styles.bigBlue}>just bigBlue</Text>
<Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
<Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
</View>
</View>
how it looks now
how I want it to look like
You can achieve this by using 2 views.
<View>
<View
style={{
backgroundColor: 'transparent',
borderStyle: 'solid',
borderLeftWidth: 200,
borderTopWidth: 30,
borderLeftColor: 'red',
borderTopColor: 'transparent',
}}
/>
<View style={{ height: 70, width: 200, backgroundColor: 'red' }}>
<Text>A</Text>
</View>
</View>

Align text and image on the same line React Native

I'm trying to align a text and an image that are wrapped in a text element in React Native.
This is what it currently looks like:
I'd like to have it aligned so it's even across.
This is the code I currently have:
<Text>
<Text
key={i}
onPress={() => this.updateCurrentFilter(i)}
style={{
paddingLeft: 10,
fontFamily: "Montserrat-Regular",
fontSize: 12,
}}
>{v.filter_name}</Text>
<Image
source={require('../assets/outline_toggle_off_black_18dp.png')}
style={{
width: 25,
height: 25,
color: 'black',
resizeMode: 'contain',
}}
/>
</Text>
Something like this should work. It's not clear why you're wrapping the outer level in a Text field instead of a View
<Text>
<View style={{flexDirection:'row', alignItems:'center'}}>
<Text style={{....}}}>foo</Text>
<Image ... />
</View>
</Text>

Am I doing something wrong in my View that the flexDirection doesn't work?

I'm trying to get the text to display to the right of the image, but for some reason flexDirection isn't working the way I am using it.
I've moved the flexDirection property around. Also have tried stuff other than View.
<View
style={{ flexDirection: 'row' }}
style={{
flex: 1,
backgroundColor: this.props.index % 2 == 0 ? '#420225' : '#69053e'
}}
>
<Image
source={{ uri: this.props.item.imageUrl }}
style={{ width: 100, height: 100, margin: 5 }}
/>
<Text style={{ color: 'white' }}>{this.props.item.textContent}
</Text>
</View>
I'm trying to get the Text to display to the right of the Image.
I change your code a little. You should not have 2 styles for your View.
<View
style={{
flex: 1,
flexDirection: 'row' ,
}}>
<Image
source={{ uri: "https://url.com" }}
style={{ width: 100, height: 100, margin: 5 }}
/>
<Text>something
</Text>
</View>

Adapt Webview height depending on the content inside

I am using Webview component in React Native to render HTML content. The problem I am facing is that I cannot make the height variable so it can adapt to any content it has.
return (
<View style={{flex: 1}}>
<Header
containerStyle={{
backgroundColor: 'white',
borderBottomColor: '#dee0e2',
borderBottomWidth: 1
}}
centerComponent={{ text: 'Noticia', style: { color: 'black' } }}
leftComponent={
<TouchableHighlight onPress={() => navigate('homeScreen')}>
<Text style={{color: '#4289f4'}}>AtrĂ¡s</Text>
</TouchableHighlight>
}
/>
<ScrollView style={{padding: 5}}>
<View style={{flexDirection: 'column'}}>
<Text style={{textAlign: 'center', fontSize: 17, fontWeight: 'bold'}}>{noticia[0].titulo}</Text>
<Image style={{width: '100%', height: 200, alignSelf: 'stretch', marginTop: 10}} source={{uri: noticia[0].imagen}} />
<View style={{height: 200, backgroundColor: 'red'}}>
<WebView
source={{html: noticia[0].articulo}}
scalesPageToFit={(Platform.OS === 'ios') ? false : true}
/>
</View>
</View>
</ScrollView>
</View>
);
If I don't give the <View>' that contents ' <Webview>' a height it does show me nothing. Tried already withflex: 1` but nothing changes.
In your parent view (root view directly under return()) style it as
height: '100%',
width: '100%'
It will adopt to any size screen.