React-native-vector-icons: Icon centered ?

Created on 29 Jan 2018  路  17Comments  路  Source: oblador/react-native-vector-icons

Yo ! I'm using this lib to render icon component and I find that the icons are not vertically centered. Maybe I'm using it wrong, but even if I render just the Icon component and put his background color to see better, the icon is clearly not centered.

image

import { Ionicon } from '@expo/vector-icons'

<Ionicon
  color='white'
  name='close'
  style={聽{ backgroundColor: 'red' }聽}
/>

I tried to put alignItems: 'center' and justifyContent: 'center' to style without success...

If I put paddingTop: 2 to style, the icon is centered

image

Most helpful comment

@basbase solution is good. And if you want to center the icon inside (because sometimes icons are not centered), you can add textAlign: 'center' to style icon

All 17 comments

Wrap the icon component in a parent view and then set that view's justifyContent style property to "center". (or alignItems if you make the flexDirection "row")

<View style={{justiftyContent:"center", alignItems:"center"}}>
   <Icon>
</View>

Same issue with Ionicons, I fixed it by reducing its height by 1.
For example:

const { name, size, color, style } = props;

<Ionicons name={name} size={size} color={color} style={{ height: size - 1, ...style }} />

Thanks @leonchabbey , I got my icons centered by setting both width and height to size:
<Ionicons name={name} size={size} color={color} style={{ height: size, width: size, ...style }} />

@basbase solution is good. And if you want to center the icon inside (because sometimes icons are not centered), you can add textAlign: 'center' to style icon

Well, I actually got the most sensible behaviour in my case by wrapping my icon in a view and setting the height on the view. This is the _only_ way that allowed me to set arbitrary vertical spacing on a centered icon! (This was with an Entypo 'arrow-right' icon, if that is important...)

@martinezguillaume your solution solved the issue for me 馃憤
<Icon size={100} style={{ textAlign: 'center' }} name={Platform.OS === "android" ? "md-checkmark-circle-outline" : "ios-checkmark-circle-outline"} color="green" />

You can put

and it going to align it self to the center

I think the right way to do this is to use containerStyle rather than style.
containerStyle ={{ flex: 1, justifyContent: 'center', alignSelf:'center'}}

{{textAlign:'center', justifyContent:'center',width:50,paddingRight:0}}

@basbase solution is good. And if you want to center the icon inside (because sometimes icons are not centered), you can add textAlign: 'center' to style icon

This is the only solution that provided pixel perfect alignment for me. Some of the other snippets here are off by 1 pixel.

It would be nice if this could make it into the official documentation.

This is the only solution that provided pixel perfect alignment for me. Some of the other snippets here are off by 1 pixel.

I've found that Android sometimes cuts off the bottom 2-3 pixels of an icon. I fixed it by also setting the lineHeight to the same size. Full code would then become:

<Ionicons 
  name={name} 
  size={size} 
  color={color} 
  style={{ 
    height: size, 
    width: size, 
    lineHeight: size, 
    textAlign: 'center',
     ...style
  }} 
/>

Setting paddingEnd: 0, to the icon style worked for me.

I solve this problem with this code

const {size} = props;

<View style={{height:size, width:size}}>
   <Icon size={size}/>
</View>

I have a more complex issue. I need the Icon centered AND wrapped inside of a Text because the Text area has a larger hit zone for an onPress event and View does not have that event at all and frankly, the Button component is lacking any uniform meaning and value:

<Text style={styles.buttonText} onPress={onFavoritePress}>
    <View style={styles.iconView}>
        <Icon style={styles.icon} name="star" />
    </View>
</Text>

I've tried all the solutions above and using just the icon as a pressable area is not acceptable for me.

@staghouse take a look at the new Pressable component https://reactnative.dev/docs/pressable
The props hitSlop is what you are looking for

@martinezguillaume Thank you ~ Perfect!

Wrap the icon component in a parent view and then set that view's justifyContent style property to "center". (or alignItems if you make the flexDirection "row")

<View style={{justiftyContent:"center", alignItems:"center"}}>
   <Icon>
</View>

@ZackTRobertson bro you have a typo in your code "justiftyContent" should be "justifyContent", people copying your code will be banging their heads on the table wondering what went wrong for hours 馃樂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

obykoo picture obykoo  路  3Comments

tharrington picture tharrington  路  4Comments

semnyqu picture semnyqu  路  3Comments

rmilejcz picture rmilejcz  路  3Comments

reactor123 picture reactor123  路  3Comments