Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw horizontal rule in React Native

Tags:

react-native

People also ask

How do you draw a horizontal line in react native?

To draw a horizontal line in React: Use the <hr /> tag and set the style prop on it. Set the height of the line and optionally set backGroundColor and color .

How do you add an HR IN React?

To draw a red horizontal line in React, we can add an hr element that has a height and background color. We create the ColoredLine component that takes the color prop. And we set the style prop to an object with the color , backgroundColor and the height properties. backgroundColor makes the line set to color .

How do you break a line in react native?

You can use \n where you want to break the line.

How do you make a dotted line in react native?

There is no easy way to make a dotted line in React Native (at least as of version 0.59, May 2019). The problem with using something like a dotted or dashed borderStyle on a <View /> component is that it will apply to all 4 sides of that view box.


You could simply use an empty View with a bottom border.

<View
  style={{
    borderBottomColor: 'black',
    borderBottomWidth: 1,
  }}
/>

I was able to draw a separator with flexbox properties even with a text in the center of line.

<View style={{flexDirection: 'row', alignItems: 'center'}}>
  <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
  <View>
    <Text style={{width: 50, textAlign: 'center'}}>Hello</Text>
  </View>
  <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
</View>

enter image description here


One can use margin in order to change the width of a line and place it center.

import { StyleSheet } from 'react-native;
<View style = {styles.lineStyle} />

const styles = StyleSheet.create({
  lineStyle:{
        borderWidth: 0.5,
        borderColor:'black',
        margin:10,
   }
 });

if you want to give margin dynamically then you can use Dimension width.


I recently had this problem.

<Text style={styles.textRegister}> ────────  Register With  ────────</Text>

with this result:

Image


I did it like this. Hope this helps

<View style={styles.hairline} />
<Text style={styles.loginButtonBelowText1}>OR</Text>
<View style={styles.hairline} />

for style:

hairline: {
  backgroundColor: '#A2A2A2',
  height: 2,
  width: 165
},

loginButtonBelowText1: {
  fontFamily: 'AvenirNext-Bold',
  fontSize: 14,
  paddingHorizontal: 5,
  alignSelf: 'center',
  color: '#A2A2A2'
},

You can also try react-native-hr-component

npm i react-native-hr-component --save

Your code:

import Hr from 'react-native-hr-component'

//..

<Hr text="Some Text" fontSize={5} lineColor="#eee" textPadding={5} textStyles={yourCustomStyles} hrStyles={yourCustomHRStyles} />