简体   繁体   English

React-Native:如何增加文本和下划线之间的空间

[英]React-Native: How to increase space between text and underline

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. 它会将我的内容的下划线创建为一些Text组件。 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! 谢谢你的帮忙!

  1. Wrap your Text in a View that has a style containing borderBottomWidth: 1 or whatever you want the thickness to be. Text换行到包含borderBottomWidth: 1或任何您想要厚度的样式的View中。

  2. Give your Text a lineHeight to adjust the spacing between the border and the content. Text提供lineHeight以调整边框和内容之间的间距。 If you have multiple lines of text, then using paddingBottom would also work. 如果你有多行文本,那么使用paddingBottom也可以。

Simple as that really. 这很简单。 Bear in mind the View border will stretch to include any padding on the View itself. 请记住, View边框将拉伸以包含View本身上的任何填充。

As of now that is not possible in React Native, cause it is also not supported in web apps ie Css. 截至目前,这在React Native中是不可能的,因为在Web应用程序即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 . 然后将borderBottomWidth添加到View并调整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 . viewStyle添加到parentView

Hope that helps! 希望有所帮助!

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. 根据我的说法,最好的方法是在<Text>之后添加一个<View /> (没有内容),并根据你想要的下划线给出顶部边框。

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). 记住:如果Text的父级有flexDirection: 'column' (默认值),这将有效。 But if it has flexDirection: 'row' , wrap both the Text & View (ie underline) in another view like <View>...</View> so the items will be arranged in a column. 但如果它有flexDirection: 'row' ,则将TextView (即下划线)包装在另一个视图中,如<View>...</View>这样项目就会排列在一列中。

How to give space between text and decoration line in react-native. 如何在react-native中给出文本和装饰线之间的空间。 Refer the attached image 请参阅附图

1 with bottom border width and 2 is decoration line 1个底边宽, 2个装饰线

具有视图bottomBorderWidth的图像

与装饰线的图象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM