react-native - react native : Perfectly align Text in circle

标签 react-native vertical-alignment

我在 React Native 中有一个圆形按钮(用 borderRadius 制作)。组件中的文本应垂直和水平居中。

Horyzontally 很好,但无论我做什么,垂直对齐似乎都失败了。即使它在小字体的大圆圈上看起来不错,小圆圈也证明它是错误的!

        <View style = {{
          alignItems:'center', 
          justifyContent:'center',  
          backgroundColor:'yellow', 
          borderColor: this.props.color,    
          width:size,   height:size, 
          borderRadius:size, 
          borderWidth:borderWidth,
        }}>
            <Text style = {{
              textAlign: 'center',  
              backgroundColor:'none', 
              fontSize:fontSize, 
              lineHeight:fontSize,
            }}>
              {this.props.title}
            </Text>
        </View>

虽然已经回答 elsewhere ,我无法正确地将文本(在这种情况下)居中。

The + is not centered Green background show it better

正如我们在 <Text> 的绿色背景图像上看到的那样- 组件,文本只是没有完美居中。即使它本身是完全对齐的......

这是世博会的小吃,整个代码减少到必要和不同的示例大小:https://repl.it/@PaulHuchner/Centered-Text-in-Circles

最佳答案

我已经尝试过仅使用文本并计算行高的先前答案。这看起来有点矫枉过正,对我不起作用。所以这是我的答案。
我使用 View 作为容器与 justifyContent:center

<View style={{
width: 40,
height: 40,

borderRadius: 20,
borderWidth: 1,
borderColor: 'black',
borderStyle: 'solid',

justifyContent: 'center'}}>
 <Text style={{fontSize: 20,textAlign: 'center'}}>20</Text></View>

关于react-native - react native : Perfectly align Text in circle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50078505/

相关文章:

javascript - 为什么我会收到 "unused default export"错误?

html - CSS垂直对齐不同的文本大小

css - 显示器之间的垂直间距 :inline divs in a fluid grid

css - -webkit-baseline-middle 和 -moz-middle-with-baseline

android - 如何在 react 原生应用程序中升级 gradle

android - react 导航 : how to detect if a screen is inside a TabNavigator or a DrawerNavigator?

php - Laravel echo 不接收事件

React-Native 链接 AddEventListener 不起作用

html - 为什么 img 标签会破坏行高的垂直对齐?

css - Flexbox - 如何在垂直方向上做空间环绕?