Vertical align text in react native (using native base)
This problem occurs especially when you are using an image or icon. I had the same problem, my solution:
<View style={{ flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
<Icon type="MaterialCommunityIcons" name="barcode" />
<Text style={{ textAlign: 'center' }}>{urun_data.barkod}</Text>
</View>
source to share
Vertical center alignment text
Case 1: There is only one text in the view Ios: use flex with alignItems / justifyContent to be more centered, Adr: height = lineHeight, includeFontPadding: false more centered
Case 2. In line view, there are two texts, and the two text fonts are different, and text with a smaller font size should not use the height.
The larger one is aligned using the above method. The smaller one can only be used with font-size, not with the line-height and height attributes. In this case, both texts can be centered.
Case 3: In Linear View, there are two texts, and the two text fonts are different, and the text with the smaller font size should use the height.
The one with the larger font size is aligned using the above method, while the smaller font size uses the indented height and includeFontPadding to achieve alignment.
And Demo
<View
style={{
flexDirection: 'row',
height: 38,
borderWidth: 1,
borderColor: '#000',
alignItems: 'center'
}}
>
<Text
style={{
fontSize: 24
}}
>
测试
</Text>
<Text
style={{
fontSize: 24,
lineHeight: 36,
height: 36,
includeFontPadding: false
}}
>
测试
</Text>
</View>
<View
style={{
flexDirection: 'row',
height: 38,
borderWidth: 1,
borderColor: '#000',
alignItems: 'center'
}}
>
<Text
style={{
fontSize: 24
}}
>
ios设备
</Text>
<Text
style={{
fontSize: 16
}}
>
更对齐
</Text>
</View>
<View
style={{
flexDirection: 'row',
height: 38,
borderWidth: 1,
borderColor: '#000',
alignItems: 'center'
}}
>
<Text
style={{
fontSize: 24,
lineHeight: 36,
height: 36,
includeFontPadding: false
}}
>
安卓
</Text>
<Text
style={{
fontSize: 12,
height: 18,
includeFontPadding: false,
paddingVertical: 2,
paddingHorizontal: 1,
paddingTop: DeviceInfo.isIOS ? 3 : 2,
}}
>
更对齐
</Text>
</View>
source to share