React Native/Flexbox align Text bottom right inline with other Text - reactjs

I'm trying to design a Message Bubble like this:
Source: Telegram iOS App
Where the Message itself is a <Text> component, and the timestamp <Text> is nested inside the Message's <Text>:
return (
<MessageBubble {...props}>
<Text style={fontStyle}>
{message.text}
<Text style={timestampStyle}>
{message.timestamp}
</Text>
</Text>
</MessageBubble>
);
The above code looks like this:
As you can see, the timestamp renders immediately at the end of the message content, but I want it to align at the bottom right.
I tried to set timestampStyle to alignSelf: 'flex-end', flex: 1, flexGrow: 1, etc but it didn't render at the bottom right.
Note: I don't want to use position: 'absolute' since that would like this:
Does anyone have an idea how I can achieve the style of the Telegram message bubble with React Native's Flexbox system?

Related

does View in react native work same as Div in HTML?

I want to render multiple element with different styles but View and child View is not working as div working in html. please guide or share some resources to learn how multiple View work in a return.
import React from 'react'
import { Button, StyleSheet, Text, View,ScrollView } from 'react-native';
export default function Home() {
return (
<View>
<View style={styles.container}>
<Text >
Books are the best Mentors
</Text>
</View>
<View>
<Text style={{backgroundColor:'red'}} >
Books are the best Mentors
</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
container2: {
flex: 1,
backgroundColor: '#fff',
position:'absolute',
bottom:0
},
});
TL;DR <div> is not the same as <View>
It looks like the possible issue here is that <div> have different default styling, and you should always remember about this. For example the default value for flexDirection in RN is column, not row like we have with <div>s.
Even though the official RN documentation about View tells us this:
View maps directly to the native view equivalent on whatever platform React Native is running on, whether that is a UIView, <div>, android.view, etc.
We should always keep in mind the specifics of the particular platform we work with.
I would also recommend you to check out this tutorial regarding Flex styling in React Native to get better experience working with it.

Custom shape and gradient in React native

I wanted to know is it possible to achieve this kind of shape and also gradient but I want to achieve gradient over the status bar. So pretty much the same like in the attachment.
For linear gradient I know how to do it, I've used https://docs.expo.io/versions/latest/sdk/linear-gradient/
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<LinearGradient
colors={['#4c669f', '#3b5998', '#192f6a']}>
<Text>
Custom shape
</Text>
</LinearGradient>
</View>
This is the result I want to achieve. https://dribbble.com/shots/5484044-Profile/attachments/1186437
You can make a custom component and manipulate the view to achieve a certain shape. Basic shapes are described here: https://codedaily.io/tutorials/22/The-Shapes-of-React-Native , but you will surely have to experiment more yourself to achieve your effect.

How to make parent's width wrap its content in React Native?

I am new to React Native so this might be a silly question.
What I am trying to get is something like this :
Where I have a parent view with 2 children, Image and Text, aligned vertically to the left. What I want is the parent view to cover only the width of its children. But instead what I get is this :
The parent view stretches to the complete width of the mobile screen.
Here is my code :
render () (<View style={{backgroundColor: '#333333'}}>
<Image source={btnImageSource} />
<Text>Some label</Text>
</View>);
Something similar to Android's 'wrap-content' width value.
You will be able to do this using flex. I found this guide to be very helpful when working with flex.
A good starting point would be to create a parent View that is flexDirection: row, justifyContent: flex-start
render () (
<View style={{justifyContent: 'flex-start', flexDirection: 'row'}}>
<View style={{backgroundColor: '#333333'}}>
<Image source={btnImageSource} />
<Text>Some label</Text>
</View>
</View>
);

React native flex issue

I am trying to setup my child component with simple view as follow:
render() {
console.log("in render")
return (
<View style={{flex:1, backgroundColor: 'skyblue'}}>
<Text>hello</Text>
</View>
It dosnt display anything. If I remove the flex it show the "hello" text with the background color as specified. what have I done wrong?
I also using navigator... maybe I need to setup something in navigator to display the view with flex?
Try adding width: '100%' to the view style.

How react native Text and Image responsive? smaller text and image in small device, bigger text and image in large device

How react native Text and Image responsive?
I only know the Flexbox can make layout responsive but seems can not apply in text and image.
e.g.Make my text and image smaller in iphone 4s, bigger in iphone 6
Thanks.
You can use Flexbox in order to make your application responsive.
For example, say you wanted to display a line of text and an image on the same line:
class ResponsiveExample extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>
Example of centered text
</Text>
<Image
resizeMode={"contain"}
style={styles.image}
source={{uri: "your image"}}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between"
},
text: {
marginLeft: 20,
flex: 1
},
image: {
marginRight: 20,
height: 400,
flex: 3,
}
});
Now the text and image will be displayed relative to the screen size. The usual hangup here is that flexDirection defaults to column, which will make things line up vertically. You can see what happens when we flip from portrait to landscape orientation:
As you can see, the text and image respond to the change in orientation.
For a better overview of Flexbox, I like to refer to the following guide:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/.
Just keep in mind that React Native defaults to a flex-direction of column, so if you want things laid out horizontally, you'll have to explicitly set it to row.
I think react-native-fit-image is useful for you.
React Native Fit Image enables you to draw responsive image component.
You can use this component as below.
<FitImage
source={{ uri: 'http://facebook.github.io/react/img/logo_og.png' }}
style={styles.fitImage}
/>
You are right to think about Flexbox, simply wrap your Text and Image inside View and make those View flexbox responsive.

Resources