Display Text components inline and wrapped inside Views - react-native

I am trying to display Text components wrapped inside View components inline. With inline, I mean that the text should be displayed next to each other at all time. This is the best "solution" that I have atm:
<View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
<View>
<Text>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</Text>
</View>
<View>
<Text style={{ fontWeight: 'bold' }}>b</Text>
</View>
<View>
<Text>cccccccccccccccccccccccccccccc</Text>
</View>
{/* Possibly an icon, or text with a different lineheight...*/}
</View>
But it doesn't work all the time! This is what it renders out:
It does work when all strings are only one line long though.
Now, I know what is going on, a View is always a rectangle, thus in my example, the view that contains the first string is as wide as the first line of the letters a and as high as the two rows of letters a.
I am looking for a way around this, while still using Views. So something like this:
<Text>
<Text>...</Text>
<Text>...</Text>
</Text>
is unfortunately not the answer. It will not work in my use-case because I need to have the ability to display superscript, which appears to only be possible by changing the line height, and I would like to be able to add some inline icons (using react-native-vector-icons).
I have been struggling with this seemingly easy issue for way too long now, so at last this cry for help. Anyone has an idea? (If there is a way to display inline superscript, that'd also work, then I can nest Text components but I guess there isn't)

you need to put text inside each other
<View style={{ flexDirection: 'row' }}>
<Text>aaaaaaaaaaaaaaaaaaaaaaaaaasdadasdadaaaaaaaaaaaaaaaaaaaaaaaa
<Text style={{ fontWeight: 'bold' }}>asdadasdab
<Text>cccccccccccccccccccccccccccccc</Text>
</Text>
</Text>
</View>

Try using display: inline-block; in the style section

Your code is missing the flex activator. This will do the trick:
<View style={{ flex: 1, flexDirection: 'row'}}>
<Text>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</Text>
<Text style={{ fontWeight: 'bold' }}>b</Text>
<Text>cccccccccccccccccccccccccccccc</Text>
</View>
You have to specify when you're working with FlexBox layouts.

Unfortunately, your question was not clear. But that's what I understood from your question
<View style={{ flex: 1}}>
<Text>aaaaaaaaaaaa</Text>
<View style={{ flexDirection: 'row', justifyContent: "flex-start"}}>
<Text style={{ fontWeight: 'bold' }}>b</Text>
<Text>ccccccccccccccccccc</Text>
</View>
</View>

I found out your solution with define multi-row.
export default class App extends React.Component {
render() {
return <View style={styles.container}>
<View ><Text >aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</Text></View>
<View ><Text style={{fontWeight: 'bold'}}>bbbbb</Text></View>
<View ><Text >ccccc</Text></View>
</View>;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column'
}
});
and you can hidden overflow text with below code
export default class App extends React.Component {
constructor(){
super()
this.state = {
mytextvar :'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
maxlimit:50
};
}
render() {
return <View style={styles.container}>
<View ><Text >{((this.state.mytextvar).length > this.state.maxlimit) ?
(((this.state.mytextvar).substring(0,this.state.maxlimit-3)) + '...') :
this.state.mytextvar}</Text></View>
<View ><Text style={{fontWeight: 'bold'}}>bbbbb</Text></View>
<View ><Text >ccccc</Text></View>
</View>;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column'
}
});

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...?

React Native Absolute Positioned Component elements onPress events not working

I created a custom dropdown component which has a transparent Scrollview with ToucableOpacity elements. However the onPress on them are not working, even though, zIndex is making them appear on top of everything else.
In the main screen, I created two elements, 1 header containing the Dropdown and 2nd one is the body which contains the rest. Its code is given below:
<View style={styles.body, {zIndex:0}}>
<View style={[styles.header,{zIndex:1}]}>
<Text style={styles.header_text}>Search In </Text>
<Dropdown items={CATEGORIES}/>
</View>
<View style={[styles.main,{zIndex:-1}]}>
<Text style={{backgroundColor:'blue'}}>I am the body</Text>
</View>
<View>
I have included the render event markup of the Dropdown element. Its code is given below:
<View style={styles.container}>
<TouchableOpacity style={styles.main_container} onPress={this.animate}>
<Text style={styles.text}>{this.state.selectedCategoryText}</Text>
<Text>-V-</Text>
</TouchableOpacity>
<View style={{zIndex:3}}>
<Animated.View style={[styles.animated_container,{opacity: this.opacity, zIndex:4}]}>
<ScrollView style={{height:60,zIndex:4}}>
{ this.data.map( (item, i)=>
<TouchableOpacity
style={{zIndex:5}}
disabled={this.state.disabled}
key={i}
onPress={()=>this.selectItem(i)}>
<Text >{item} {i}</Text>
</TouchableOpacity>
)}
</ScrollView>
</Animated.View>
</View>
</View>
Styles for Dropdown are:
container:{
zIndex:2
},
main_container:{
flexDirection: 'row',zIndex:2
},
text:{
fontFamily: fontStyles.PoppinsRegular,
fontSize: moderateScale(14)
},
animated_container:{
backgroundColor: colors.secondaryWhiteColor,
position: 'absolute',
top: '100%',
width: '100%',
zIndex:3
}
Screenshot is given as. zIndex makes the elements appear on top but i cannot trigger onPress on Toucable opacities.

React Native add border to text

I want to add a border around my Text component. I've done the following, however I only want the border around the text, not the full width of the View.
<View style={{borderWidth: 1}}/>
<Text>A DIFFERENT EXPERIENCE</Text>
</View>
Is this possible?
Yes it is possible for that you need to wrap it in another view like this:
<View style={styles.container}>
<View style={styles.inner}>
<Text>A DIFFERENT EXPERIENCE</Text>
</View>
</View>
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
borderWidth:4
},
inner:{
borderWidth:4
}
Cheers:)

Superscript Text in React Native

I would like to style text as superscript in React Native. How would this be accomplished? Is there a way to make use of the Javascript sup() string method to return a string as superscript within a <Text> object?
I got this working for me using a view container and flex.
<View style={{flexDirection: 'row', alignItems: 'flex-start'}}>
<Text style={{fontSize: 20, lineHeight: 30}}>10</Text>
<Text style={{fontSize: 11, lineHeight: 18}}>am</Text>
</View>
Here is the link to this in action https://repl.it/Haap/0
Cheers!
i just do this to achieve superscript in my case
<View style={{ flexDirection: 'row' }}>
<Text style={{ fontSize: 30 }}>e</Text>
<Text style={{ fontSize: 10 }}>x</Text>
</View>
Just use fontSize, lineHeight, textAlignVertical:
<Text style={{fontSize:20, lineHeight:22}}>
foo
<Text style={{fontSize:20 * 1.6, lineHeight:22 * 1.1, textAlignVertical: 'top'}}>
bar
</Text>
</Text>
Javascripts sub() function will only surround your text with <sub></sub> tags and they are recognized as text in RN. You would need to build your own function like:
export default class Test extends Component {
sub = (base, exponent) => {
return <View style={{flexDirection: 'row'}}>
<View style={{alignItems: 'flex-end'}}>
<Text style={{fontSize: 13}}>{base}</Text>
</View>
<View style={{alignItems: 'flex-start'}}>
<Text style={{fontSize: 10}}>{exponent}</Text>
</View>
</View>
}
render() {
return(
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>{(() => 'hello'+'world'.sub())()}</Text>
{(() => this.sub('hello','world'))()}
{(() => this.sub('2','6'))()}
</View>
);
}
}
The best method I found for this problem is not ideal, but it works cross-platform. The character I needed to superscript is the ® which some fonts have superscripted by default. So, I simply included a similar font family with the superscripted ® and it worked like magic.

How to vertically aligned my image to top in my React-native component

I'd like to top aligned my image in React-native. But don't know what to do. Here is my layout and its style:
class layout extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<Text style={styles.left}>
Left Button
</Text>
<Text style={styles.title}>
This is my title
</Text>
<Text style={styles.right}>
Right Button
</Text>
</View>
<View style={styles.content}>
<Image style={styles.image}
source={require('./back.jpg')}
resizeMode="contain"
></Image>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
toolbar: {
backgroundColor: '#B5AC3a',
height:64,
flexDirection:'row'
},
left: {
marginTop:30,
fontSize: 14,
textAlign: 'center'
},
right: {
marginTop:30,
fontSize: 14,
textAlign: 'center'
},
title: {
marginTop:30,
flex: 1,
fontSize: 14,
textAlign: 'center'
},
content: {
backgroundColor:'#ffecff'
},
image: {
width: Dimensions.get('window').width - 20,
margin: 10,
alignSelf:'center'
}
});
Here is the result:
Apparently there are a lot of space between my image to top of my screen. I tried: flex-start. But doesn't work. What should I do? Thanks.
P.S: It's fine if I use View instead of Image like this:
class layout extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<Text style={styles.left}>
Left Button
</Text>
<Text style={styles.title}>
This is my title
</Text>
<Text style={styles.right}>
Right Button
</Text>
</View>
<View style={styles.content}>
<View style={styles.messageBox}>
<Text>This is line one</Text>
<Text>This is line two</Text>
</View>
</View>
</View>
);
}
}
Result:
Your resizeMode='contain' on your image is conflicting with the dimensions you want to define in styles.image.
Simply remove resizeMode='contain' in your <Image> properties and it should work.
Something else: I would strongly advise you, depending on what you're trying to do, to use a ScrollView instead of the simple View when you have extendable content (namely anything that isn't completely static). This seems to be what you need.