Vertical text in react native is cut by it's original height (using transform) - react-native

I have the following code in which I would like the black boxed text to be placed vertically, either centered or aligned to the top, and be cut by the end of the corresponding text view (sample text).
It seems like even though the text is transformed, it still measures its original height and thus shows only the first letter with ...
adding width:50 to the transformed view, will show better results, but the number of lines in the sample text is changing (and also the font sizes).
this is the result I'm getting:
And this is the required result (using paint.net ;)):
Does anyone have an idea how to do it?
Thanks!
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
class SideComponent extends Component{
render(){
return (
<View style={{flex:1, alignItems:'center', justifyContent:'center', borderColor:'black', borderWidth:0.5, width:20,}}>
<View style={{transform: [{ rotate: '90deg'}]}}>
<Text numberOfLines={1} style={{borderColor:'blue'}}>{this.props.text}</Text>
</View>
</View>
)
}
}
class LineComponent extends Component{
render(){
return (
<View style={{flex:1}}>
<Text numberOfLines={2} style={{backgroundColor:'yellow'}}>Sample text</Text>
<Text numberOfLines={2} style={{backgroundColor:'yellow'}}>Sample text</Text>
<Text numberOfLines={2} style={{backgroundColor:'yellow'}}>Sample text</Text>
</View>
)
}
}
export default class App extends Component{
render() {
return (
<View style={styles.container}>
<View style={{flexDirection:'row'}}>
<SideComponent text="Short"/>
<LineComponent/>
</View>
<View style={{flexDirection:'row'}}>
<SideComponent text="LongTextLong" style={{}}/>
<LineComponent/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
flexDirection: 'column',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

I tried something like this for vertical transformation:
<View style={{flexDirection:'row'}}>
<View style={{flex:0.1 }}>
<View style={{left:-130,width:300,height:20,alignItems:'flex-start',justifyContent:'flex-start',transform: [{ rotate: '270deg'}]}}>
<Text numberOfLines={1} style={[AppStyles.regularFontText,{fontSize:12,color:'#8160e5'}]}>Number of Users</Text>
</View>
</View>
<View style={{flex:0.9}}>
<BarChart
style={{height:200,width:width-100[enter image description here][1]}}
data={this.state.line_data}
chartDescription={{ text: '' }}
xAxis={this.state.xAxis}
animation={{durationX: 2000}}
legend={this.state.line_legend}
gridBackgroundColor={processColor('#ffffff')}
drawBarShadow={false}
drawValueAboveBar={true}
drawHighlightArrow={true}
onSelect={this.handleSelect.bind(this)}
highlights={this.state.highlights}
onChange={(event) => console.log(event.nativeEvent)}
/>
</View>
</View>

Related

text above row of buttons in React Native

This should be relatively easy for a React Native expert. I simply wish to display some text, and then under the text should be a row of buttons. Beginning with the row of buttons, this seems to work fine:
constructor(props) {
super(props);
this.state = {
titleText: "Bird's Nest",
bodyText: "This is not really a bird nest."
};
}
render() {
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
); //return
} //render
const styles = StyleSheet.create({
page:{
flex:1,
backgroundColor: "#000",
paddingTop:50,
color: 'white'
},
container: {
flex: 1,
backgroundColor: "#000",
flexDirection: 'row',
alignItems: 'center',
justifyContent: "center"
},
buttonContainer: {
flex: 1,
},
container1: {
backgroundColor: 'powderblue',
height : 100
},
container2: {
backgroundColor: 'grey',
height : 100
},
baseText: {
fontFamily: "Cochin"
},
titleText: {
fontSize: 20,
fontWeight: "bold",
color: 'white'
}
});
When I attempt to add the text...it is functional however the text is positioned before the row of buttons instead of placement over/above the row of buttons. Here is the same code inclusive of the text:
...
render() {
return (
<View style={styles.container}>
<Text style={styles.baseText}>
<Text style={styles.titleText} onPress={this.onPressTitle}>
{this.state.titleText}
{"\n"}
{"\n"}
</Text>
<Text numberOfLines={5}>{this.state.bodyText}</Text>
</Text>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
); //return
} //render
...
So basically the 'text' is rendered and then the 4 buttons are rendered with everything all 'crushed' together...all within the same row. There probably needs to be some sort of 'column' for the text however I have no idea how to combine 'column' and 'row' (necessary for the buttons) in React Native (within the same page 'container'). Or perhaps there is some other way to do it?
In addition, it would be nice if the row of buttons appeared along the bottom of the device screen instead of centered. I had changed the 'alignItems' in the 'styles.container' to 'baseline' (before I added any text) however that did not accomplish the desired result. I thank you in advance for any advice.
UPDATE:
At this point I have modified my code above and what I have now is something more along the lines of what I am seeking...although not perfect. There is probably a better way however here is what my code looks like now:
import BoxContainer from './BoxContainer';
render() {
return (
<View style={styles.page}>
<BoxContainer style={styles.container1}>
<Text>Bunch of text here.</Text>
</BoxContainer>
<BoxContainer style={styles.container2}>
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
</BoxContainer>
</View>
); //return
} //render
The code for 'BoxContainer' is here, a resource I found on the Internet somewhere:
import React from 'react';
import { StyleSheet, View} from 'react-native';
const BoxContainer = props => {
return (
<View style={{...styles.boxContainer, ...props.style}}>
{props.children}
</View>
);
};
const styles = StyleSheet.create({
boxContainer:{
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.8,
shadowRadius: 2,
height:200,
margin: 20,
alignItems: 'center',
justifyContent: 'center'
}
});
export default BoxContainer;
As mentioned, this is close to what I am seeking however I wish I could find a way to do it without the structured box containers...or perhaps that is necessary in React Native...?

Nested Texts not aligning (React Native)

I have this basic setup in React Native:
class App extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
<View>
<Text style={styles.textHighlighting}>
Testing
</Text>
</View>
.
</Text>
<Text style={styles.textContainer}>
<View>
<Text style={styles.textHighlighting}>
Again
</Text>
</View>
!
</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
textContainer: {
fontSize: 50
},
textHighlighting: {
backgroundColor: "yellow",
borderRadius: 10,
fontSize: 50
}
})
I am wrapping the inner text in a view because I want it to be highlighted and to be able to add a borderRadius. This seems to break when I don't have the View. But the text I don't have within the View seems to align itself lower vertically than the highlighted text.
It seems to work fine on IOS. Just not on Android.
Check this expo snack https://snack.expo.dev/#gaurav1995/carefree-truffle
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default function App() {
return (
<View style={styles.container}>
<View style={styles.textContainer}>
<Text style={styles.textHighlighting}>
Testing
</Text>
<Text style={styles.newFont} >.</Text>
</View>
<View style={styles.textContainer}>
<Text style={styles.textHighlighting}>
Again
</Text>
<Text style={styles.newFont} >!</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
textContainer: {
flexDirection:'row',
marginVertical:5,
padding:2
},
textHighlighting: {
backgroundColor: "yellow",
borderRadius: 10,
fontSize: 50,
},
newFont:{
fontSize:50,
marginLeft:2
}
});
check this out
render() {
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
<Text style={styles.textHighlighting}>Testing</Text>.
</Text>
<Text style={styles.textContainer}>
<Text style={styles.textHighlighting}>Again</Text>!
</Text>
</View>
);
}
}

react-native position element at the bottom

I am trying to move a react native component called "forum" to the bottom of the screen, I tried position: fixed, bottom:0 but it didn't work
I also tried 'flex-end' and still didn't work
any idea what's wrong?
here is the expo code:
import React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { DrawerNavigator } from 'react-navigation';
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Home</Text>
</View>
);
}
}
class Enquiry extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Enquiry</Text>
</View>
);
}
}
class Batches extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Batches</Text>
</View>
);
}
}
class Forum extends React.Component {
render() {
return (
<View style={{ flex:1,justifyContent:'center',alignItems: 'center',position:'absolute',bottom:0}}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold',marginBottom:36}}>Forum</Text>
</View>
);
}
}
const MyDrawerNavigator = new DrawerNavigator(
{
Home: HomeScreen,
Batches:Batches,
Enquiry:Enquiry,
Forum:Forum,
},
{
drawerBackgroundColor: 'rgba(255,255,255,.9)',
contentOptions: {
activeTintColor: '#fff',
activeBackgroundColor: '#6b52ae',
},
}
);
export default MyDrawerNavigator;
I am not allowed to share images but click the link to see
enter image description here
The valid position option in react native is relative or absolute. The default value is relative.
If you want scroll content and fix position of Forum in your screen, you have to separate your view hierarchy like the following and use absolute position.
<View>
<ScrollView>
...anything content
</ScrollView>
<Forum style={{position:'absolute', bottom: 0}}/>
</View>
<...>
<MyDrawerNavigator .../>
<Forum style={{position:'absolute', bottom: 0}} .../>
<.../>

How to add text above the image?

I use react-native-swiper and i want to add a text above my image.
I try to set style container image paragraph, it is still not working.
What should i do about the style setting ?
Any help would be appreciated. Thanks in advance.
import React, { Component } from 'react';
import { View, Text, Image, Dimensions } from 'react-native';
import Swiper from 'react-native-swiper';
const dimensions = Dimensions.get('window').width;
class About extends Component {
render() {
return (
<View style={{ flex: 1 }}>
<Swiper
style={styles.wrapper}
height={200}
paginationStyle={{bottom: 10}}
>
<View style={styles.container}>
<Image style={styles.image} source={require('../img/home_service_bg1.jpg')} />
<Text style={styles.paragraph}>How to let me show above the img.</Text>
</View>
<View style={{ flex: 1 }}>
<Image source={require('../img/home_service_bg2.jpg')} style={styles.img}/>
</View>
</Swiper>
<View style={{ flex: 2 }}>
<Text>Hi</Text>
</View>
</View>
);
}
}
const styles = {
wrapper: {
},
container: {
flex: 1,
alignItems: 'stretch',
justifyContent: 'center',
},
image: {
flexGrow:1,
height:null,
width:null,
alignItems: 'center',
justifyContent:'center',
},
paragraph: {
textAlign: 'center',
},
};
export default About;
Here is my situation now:
I hope it becomes like this:
position="absolute" will make it work as Android RelativeLayout.
If you want set view on top set marginTop to 0.
After seeing your requirement. You can simply use ImageBackground
<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
<Text>Inside</Text>
</ImageBackground>
See facebook docs

React Native: Align two TextInputs side by side

I am just starting out with React Native and I am developing an app using RN ... I am bit stuck here ... I have a form in one of the app's component that have couple of TextInputs aligned side by side like in the image below
Here is the code that I have written trying to achieve the above design.
import React, {Component} from 'react';
import {View, Text, StyleSheet, TextInput, TouchableHighlight} from 'react-native';
export default class AddItems extends Component {
onAdd() {
alert('Hello');
}
render() {
return (
<View style={addItemStyles.wrapper}>
<View>
<Text>Item to give cash credit for:</Text>
<View>
<View>
<TextInput placeholder="Test" style={{justifyContent: 'flex-start',}} />
</View>
<View>
<TextInput placeholder="Test" style={{justifyContent: 'flex-end',}} />
</View>
</View>
</View>
</View>
);
}
}
const addItemStyles = StyleSheet.create({
wrapper: {
padding: 10,
backgroundColor: '#FFFFFF'
},
inputLabels: {
fontSize: 16,
color: '#000000',
marginBottom: 7,
},
inputField: {
backgroundColor: '#EEEEEE',
padding: 10,
color: '#505050',
height: 50
},
inputWrapper: {
paddingBottom: 20,
},
saveBtn: {
backgroundColor: '#003E7D',
alignItems: 'center',
padding: 12,
},
saveBtnText: {
color: '#FFFFFF',
fontSize: 18,
}
});
But instead I am getting the view like this.
I know this could be a minor thing but as I said above that I am just starting with React Native so you can consider me as a noob ... Thanks in advance for your help. Looking forward to your answers.
use "flexDirection" in style
render() {
return (
<View style={addItemStyles.wrapper}>
<View>
<Text>Item to give cash credit for:</Text>
<View style={{flexDirection:"row"}}>
<View style={{flex:1}}>
<TextInput placeholder="Test" style={{justifyContent: 'flex-start',}} />
</View>
<View style={{flex:1}}>
<TextInput placeholder="Test" style={{justifyContent: 'flex-end',}} />
</View>
</View>
</View>
</View>
);
}