16

I have followed styles:

const styles = StyleSheet.create({
    title: {
        textDecorationLine: 'underline',
        textDecorationStyle: 'solid',
        textDecorationColor: '#000'
    }
});

and it creates the underline for my content into some Text component. But it seems that this underline is too close to the text decorated with it.

Can I increase this distance in some how?

Thank you for helping!

Alex Belets
  • 356
  • 1
  • 4
  • 15
  • 1
    Possible duplicate of [How to increase the gap between text and underlining in CSS](https://stackoverflow.com/questions/1734618/how-to-increase-the-gap-between-text-and-underlining-in-css) – Thomas Jun 21 '17 at 14:42
  • 6
    No. It's all about react-native. – Alex Belets Jun 21 '17 at 14:54

4 Answers4

26
  1. Wrap your Text in a View that has a style containing borderBottomWidth: 1 or whatever you want the thickness to be.

  2. Give your Text a lineHeight to adjust the spacing between the border and the content. If you have multiple lines of text, then using paddingBottom would also work.

Simple as that really. Bear in mind the View border will stretch to include any padding on the View itself.

G0dsquad
  • 4,230
  • 1
  • 17
  • 22
2

As of now that is not possible in React Native, cause it is also not supported in web apps i.e Css.

Link here

But there is a work around to this.

Create react View wrapper over the Text you want to adjust the underline. And then add borderBottomWidth to View and adjust the distance of underline from Text paddingBottom.

const styles = StyleSheet.create({

    viewStyle : {
      borderBottomWidth: 10, // whatever width you want of underline
   }
    title: {
        paddingBottom: 4, // Adjust the distance from your text view.
    }
});

Add the viewStyle to your parentView.

Hope that helps!

Prabodh M
  • 2,132
  • 2
  • 17
  • 23
0

According to me, the best possible way to do this is to add a <View /> (without content) after the <Text> and give top-border as you want your underline to be.

For example:

<Text style={{ color: 'blue' }}>Categories</Text>
<View style={{ 
    height: 0,               // height is '0' so that the view will not occupy space
    width: 100,              // as much as you want to 'Stretch' the underline
    borderTopColor: 'blue', 
    borderTopWidth: 2,       // 'Thickness' of the underline
    marginTop: 15 .          // 'Gap' between the content & the underline 
}} />

REMEMBER: This will work if the parent of your Text has flexDirection: 'column' (which is default value). But if it has flexDirection: 'row', wrap both the Text & View (i.e. underline) in another view like <View>...</View> so the items will be arranged in a column.

TalESid
  • 2,304
  • 1
  • 20
  • 41
0

How to give space between text and decoration line in react-native. Refer the attached image

1 with bottom border width and 2 is decoration line

Image with view bottomBorderWidth

Image with decorationline

sejn
  • 2,040
  • 6
  • 28
  • 82