21

I need to display a dotted line in a view

I have tried borderTopWidth: 1, borderStyle: 'dashed' for a view.

Alice Bob
  • 223
  • 1
  • 2
  • 6
  • 2
    NOTE: As of writing, applying `borderStyle` to a single border edge is broken in React Native 0.63.2 but there is an open PR to fix this limitation: https://github.com/facebook/react-native/pull/29099 – Joshua Pinter Jul 30 '20 at 17:36
  • 1
    In 2021 - guess what? - that PR is still sitting open, completely ignored by the React Native team, accumulating dozens of "Hey what's happening here?" comments. Sigh... at least the bot hasn't closed it yet... – user56reinstatemonica8 Jan 26 '21 at 14:52

12 Answers12

26

Just add borderRadius it will work

<View style={{
    borderStyle: 'dotted',
    borderWidth: 1,
    borderRadius: 1,
  }}>
</View>
Masuk Helal Anik
  • 2,155
  • 21
  • 29
13

Or you can just do this if you want a dashed horizontal line:


    <Text ellipsizeMode="clip" numberOfLines={1}>
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      - - - - - - - - - - - - - - - - -
    </Text>
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Omar Borji
  • 223
  • 2
  • 9
  • 8
    If you're going to do this hack, be sure to test it on screen readers (Android TalkBack and iOS VoiceOver), to make sure screen reader users will not need to listen through something like "Hyphen hyphen hyphen hyphen hyphen hyphen hyphen hyphen hyphen hyphen ..." - https://reactnative.dev/docs/accessibility – user56reinstatemonica8 Jan 26 '21 at 14:49
  • just to add `accessible={false}` prop @user56reinstatemonica8 – Vasyl Nahuliak Feb 24 '23 at 16:11
7

write your code like this:

<View style={{ height: 1, width: '100%', borderRadius: 1, borderWidth: 1, borderColor: 'red', borderStyle: 'dotted' }} />

In case you don't like that, this is the ultimate answer (I wrote this with 'dashed' borderStyle in order to be seen more clearly.

<View style={{ height: 1, width: '100%', borderRadius: 1, borderWidth: 1, borderColor: 'red', borderStyle: 'dashed', zIndex: 0, }}>
  <View style={{ position: 'absolute', left: 0, bottom: 0, width: '100%', height: 1, backgroundColor: 'white', zIndex: 1 }} />
</View>
Amir Gorji
  • 2,809
  • 1
  • 21
  • 26
5

You can try this one also, it give you perfect dotted line.

<View style={{borderWidth:0.3, borderStyle:'dashed', borderRadius:1,borderColor:'black'}}></View>
Pravin Ghorle
  • 606
  • 7
  • 7
5
import React from "react";
import * as Svg from "react-native-svg";

type IVerticalDashedLine = {
  height: number;
  width: number;
  color: Svg.Color;
};

export default function VerticalDashedLine({
  height,
  width,
  color,
}: IVerticalDashedLine) {
  return (
    <Svg.Svg height={height} width={width} style={{ alignSelf: "center" }}>
      <Svg.Line
        stroke={color}
        strokeWidth={width}
        strokeDasharray="3, 2"
        x1="0"
        y1="0"
        x2="0"
        y2={height}
      />
    </Svg.Svg>
  );
}
Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50
4

You can use below library that will help you to achieve your design as dotted.

react-native-dash

A super simple component for react-native to draw customisable dashed lines

Installation

npm i --save react-native-dash

Usage

import Dash from 'react-native-dash';

//draws a horizontal dashed line with defaults. Also works with flex
render() {
    return <Dash style={{width:100, height:1}}/>
}

//draws a vertical dashed line with defaults.
render() {
    return <Dash style={{width:1, height:100, flexDirection:'column'}}/>
}

You can get more information then may visit above link.

Thank you

Hardik Virani
  • 1,687
  • 7
  • 23
  • 36
  • 3
    **2022 update:** This library is not updated in a while and breaks the RN 69+. My suggestion is to consider other alternatives. – jhpg Sep 26 '22 at 20:59
3

TL;DR: If you need any kind of control, and not a hacky solution, use a third-party solution like react-native-dash.

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. That means you will get super tight groupings of dots and dashes, with no out-of-the-box solution for controlling the dash or dot spacing or size. It's fine for doing a simple dotted or dashed box, but not a line.

Erik
  • 227
  • 3
  • 5
  • unfortunately the lib doesn't seem to be maintained anymore, and its usage of react-native-measureme has proven limitations (see [issue](https://github.com/obipawan/react-native-dash/issues/30)) – Amaury Liet Sep 04 '20 at 08:31
  • 1
    For those of you with issues using `react-native-dash`, I have written my own (dependency free) version using hooks and functional components, called [react-native-dashed-line](https://www.npmjs.com/package/react-native-dashed-line) – Conor Watson Apr 26 '21 at 09:00
3

Thanks @Amir Gorji for this one. If you want only one side of a view's borders to be dashed, the bottom one for example, use this:

<View 
    style={{ 
        height: 100,
        backgroundColor: 'white',
        borderWidth: 1,
        borderColor: 'black',
        borderRadius: 1,
        borderStyle: 'dashed',
        zIndex: 0
    }}
>
    <View style={{ position: 'absolute', left: -1, top: -1, width: '100%', height: 1, backgroundColor: 'white', zIndex: 1 }} />
    <View style={{ position: 'absolute', left: -1, top: -1, width: 1, height: '100%', backgroundColor: 'white', zIndex: 1 }} />
    <View style={{ position: 'absolute', right: -1, top: -1, width: 1, height: '100%', backgroundColor: 'white', zIndex: 1 }} />
</View>

It's not perfect, but it's the best I could find.

P.S. react-native-dash does not work.

instanceof
  • 1,404
  • 23
  • 30
2

My Solution for dashed line works for horizontal and portrait mode that uses '-' (negative symbol).

Adjust fontSize and letterSpacing to get the desired result as the fontFamily for this example is OpenSans. The result might be different.

import React from 'react';
import {color} from '../../theme';
import {View} from 'react-native';
import {Text} from '../text';

const CONTAINER = {
  flexDirection: 'row',
  justifyContent: 'center',
  overflow: 'hidden',
  width: '100%',
};

const DASHED = {
  color: color.primary,
  letterSpacing: -1.87,
  fontSize: 18,
};

export function Divider() {
  return (
    <View style={CONTAINER}>
      {[...Array(60)].map((_, ind) => {
        return (
          <Text key={ind} style={DASHED}>
            {' '}
            --{' '}
          </Text>
        );
      })}
    </View>
  );
}

Kailash Uniyal
  • 878
  • 1
  • 10
  • 14
2

A few people have suggested using the react-native-dash library, which now seems to be unmaintained and requires third party dependencies (which can potentially cause issues).

An alternative solution is the react-native-dashed-line package which has been written from the ground up, using hooks and functional components. It has no dependencies and can be used as follows

<DashedLine dashLength={5} />

<DashedLine dashLength={10} dashThickness={8} />

react-native-dashed-line examples

Conor Watson
  • 567
  • 1
  • 7
  • 28
1

Here is a solution with no library.

dashed-line-react-native

gamingumar
  • 979
  • 8
  • 14
0

I use the Text component with a trick to draw a dashed line like this :

 <Text ellipsizeMode="clip" numberOfLines={1} style={{ opacity: 0.1 }}>
          {Array.from(Array(100).keys()).map((each) => {
            return "-    ";
          })}
 </Text>
Mehdi Faraji
  • 2,574
  • 8
  • 28
  • 76