I have try to use textAlign: 'justify' property in react native.TextAlign justify doesn't work on Android.
React native version: "react-native": "0.59.5"
import React from 'react';
import { Image, View } from 'react-native';
import { Text, Button } from 'native-base';
import { connect } from 'react-redux';
import EStyleSheet from 'react-native-extended-stylesheet';
import { colors } from '../styles';
class Login extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.contentCenter}>
<Text style={[styles.titleContent,styles.fontBold]}>Simple Secure</Text>
<Text style={[styles.titleContent,styles.fontBold]}>Reliable Messaging</Text>
</View>
</View>
);
}
}
const styles = EStyleSheet.create({
titleContent: {
color:'#62778c',
fontSize: '1.4rem',
textAlign: 'justify',
}
});
const mapStateToProps = (state) => {
return {};
};
export default connect(mapStateToProps)(Login);
checkout the offical document:
textAlign: enum('auto', 'left', 'right', 'center', 'justify')
Specifies text alignment. The value 'justify' is only supported on iOS and fallbacks to left on Android.
As mentioned above android does not support text justification but you can go around this by adding the html text inside WebView like this:
<WebView
source={{ html: "<p style='text-align:justify;'>Your justified text</p>" }}
/>
Related
I am making a upload product for selling .
And I had make a component for receive the input number(units or price) from the user,
and in the main page will need to count the total price : units * price ..
However ,I get stucked here ... I don't know how I am able to get the value from the component ..
I have attached my code . Could you please take a look ? Thank you so much !!
Component for putting number
import React from 'react';
import { StyleSheet, View } from 'react-native'
import { TextInput } from 'react-native-gesture-handler';
import colors from '../config/colors';
function TryInputNum({children}) {
return (
<View style={styles.container}>
<TextInput placeholder="Enter a number">{children}</TextInput>
</View>
);
}
const styles = StyleSheet.create({
container :{
width : 150,
height : 200,
backgroundColor : colors.white,
}
})
export default TryInputNum;
The main page
import React from 'react';
import { StyleSheet, View,Text } from 'react-native'
import TryInputNum from '../components/TryInputNum';
import colors from '../config/colors';
function TryCountScreen({children}) {
return (
<View style={styles.container}>
<Text>{children}</Text>
<TryInputNum/>
</View>
);
}
const styles = StyleSheet.create({
container : {
flex :1,
backgroundColor : colors.primary,
justifyContent : 'center',
alignItems : 'center',
}
})
export default TryCountScreen;
You can pass onChangeText function from the children and access it as a prop inside the main component. In the onChangeText you can return the text value from the TextInput component and handle it inside the main component. It would like like this
function TryInputNum({children,onChangeText}) {
return (
<View style={styles.container}>
<TextInput onChangeText={value => onChangeText(value)} placeholder="Enter a
number">{children}</TextInput>
</View>
);
}
and then deal with the value from the main screen like this
import React from 'react';
import { StyleSheet, View,Text } from 'react-native'
import TryInputNum from '../components/TryInputNum';
import colors from '../config/colors';
function TryCountScreen({children}) {
return (
<View style={styles.container}>
<Text>{children}</Text>
//Here you have access to that text value thats written inside the
// TextInput component
<TryInputNum onChangeText={text => console.log(text)}/>
</View>
);
}
const styles = StyleSheet.create({
container : {
flex :1,
backgroundColor : colors.primary,
justifyContent : 'center',
alignItems : 'center',
}
})
export default TryCountScreen;
I am using below package currently. But I am failing to add style customisation in my AdMobBanner component. Please tell if any other package might be useful for Google Ads customisation or any other Platform Ads that supports customisation.
https://www.npmjs.com/package/react-native-admob
Please click on this link to see my current O/P. I want to remove border and add labels and buttons below it. Is it possible?
import React, { PureComponent } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { AdMobBanner } from 'react-native-admob';
const BannerExample = ({ style, title, children, ...props }) => (
<View {...props} style={[styles.example, style]}>
<View>{children}</View>
</View>
);
const adUnitID = 'ca-app-pub-3940256099942544/2934735716';
export default class GoogleAdsCompo extends PureComponent {
render() {
return (
<ScrollView>
<BannerExample title="Smart Banner">
<AdMobBanner
adSize="mediumRectangle"
adUnitID={adUnitID}
ref={el => (this._smartBannerExample = el)}
/>
</BannerExample>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
example: {
paddingVertical: 10,
justifyContent: 'center',
alignItems: 'center',
},
title: {
margin: 10,
fontSize: 20,
},
});
I want to display a custom message after successful record update when boolean value of recordUpdateSuccess become true and after 3seconds it should disappear.
{recordUpdateSuccess ? this.renderRecordUpdatedSucess() : null}
I have function to display message:
renderRecordUpdatedSucess = () => (
<View style={styles.sucessAlert}>
<Text style={styles.sucessAlert}>Record updated successfully.</Text>
</View>
)
I tried to use setTimeout() to display message but not working.
Any idea to acheive this one.
I don't want to use Toast, any third party library for this one.
Custom flash message (No external Library)
Working Example: https://snack.expo.io/#msbot01/1dcddc
import * as React from 'react';
import { Text, View, StyleSheet, TouchableOpacity } 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 class App extends React.Component {
constructor(props) {
super(props);
this.state = {
flashMessage: false
}
}
onPress(){
this.setState({
flashMessage: true
},()=>{setTimeout(() => this.closeFlashMessage(), 3000)})
}
closeFlashMessage(){
this.setState({
flashMessage: false
})
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={()=>{this.onPress()}}>
<Text>Click Me</Text>
</TouchableOpacity >
{this.state.flashMessage==true?
<View style={styles.flashMessage}>
<Text style={{color:'white'}}>This is custom Flash message</Text>
</View>
:
null
}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
flashMessage:{
position:'absolute',
backgroundColor:'green',
width:'100%',
justifyContent:'center',
alignItems:'center',
height:40,
top:0
}
});
New to React Native, having a go at an app idea. Basically I'm just trying to create an TextInput element that appears when I press a Button. Below is my attempt. I'm trying to take advantage of state within my class, but something is off.
Expo is throwing an error of 'null is not an object (evaluating 'this.state.isShowingText')'.
Any ideas?
import React, { Component } from 'react';
import { TextInput, Alert, Button, ScrollView, Text, View, StyleSheet } from 'react-native';
export default class CoolComponent extends Component {
render() {
const nameAdd = () =>{
state = { isShowingText: true };
}
return (
<View style={{ alignItems: 'center', top: 50 }}>
<Title>Some Title</Title>
{this.state.isShowingText ? <TextInput></TextInput> : null}
<ScrollView></ScrollView>
<Button
title="Press me"
onPress={nameAdd}
/>
</View>
);
}
}
The way you handling state is wrong
import React, { Component } from 'react';
import { TextInput, Button, ScrollView, View } from 'react-native';
export default class CoolComponent extends Component {
state = {
isShowingText: false
}
nameAdd = () => {
this.setState({
isShowingText: true
})
}
render() {
return (
<View style={{ alignItems: 'center', top: 50 }}>
{this.state.isShowingText ? <TextInput style={{ width: '50', height: '50', borderWidth: 1 }}></TextInput> : null}
<ScrollView></ScrollView>
<Button
title="Press me"
onPress={this.nameAdd}
/>
</View>
);
}
}
Hope this helps you. Feel free for doubts.
I have a custom component that is simply a Text component that receives various props for styling. It works perfectly on iOS but on Android the text of the component doesn't shows.
This is the code of the component:
import React, { Component } from 'react';
import { Text, StyleSheet } from 'react-native';
export default class TextLabel extends Component {
render() {
const colors = {
red: '#CF243E',
lightRed: '#E9BBC0',
white: '#FFFFFF',
default: '#434A54'
}
const weights = {
bold: 'Raleway-Bold',
default: 'Raleway-Regular'
}
const styles = StyleSheet.create({
textStyles: {
fontFamily: this.props.weight ? weights[this.props.weight] : weights.default,
fontSize: this.props.size ? this.props.size : 16,
textTransform: this.props.case ? this.props.case : 'none',
color: this.props.color ? colors[this.props.color] : colors.default
},
additionalStyles: this.props.additionalStyles ? this.props.additionalStyles : {}
})
return (
<Text style={[styles.textStyles, styles.additionalStyles]}>
{this.props.children}
</Text>
);
}
}
I then use it like this in other components:
import React, { Component } from 'react';
import { View } from 'react-native';
import TextLabel from '../UI/TextLabel';
export default class Resolution extends Component {
render() {
return (
<View style={styles.wrapper}>
<TextLabel
size={21}
weight={'bold'}
additionalStyles={{ marginTop: 30, marginBottom: 10, textAlign: 'center'}}
>
Some random text
</TextLabel>
</View>
);
}
}
But on Android it doesn't renders the text while on iOS it works perfectly. What could be going on?
Thanks in advance.
As it turns out this is due to the use of the textTransform property which currently has a bug on Android that causes the text to disappear if you set this property in a component's style.
The solution for now is to either not use it or use string.toUpperCase() as workaround.
More info on this bug here: https://github.com/facebook/react-native/issues/21966