circle outline for fontAwesome icons in react native

前端 未结 3 1636
谎友^
谎友^ 2021-01-28 03:16

I want to use the fontAwesome + icon such that it is in the middle of a circle. I want to use it as one icon item. I read that we can use it along with the circle icon and place

相关标签:
3条回答
  • 2021-01-28 03:37

    The JSFiddle example that you posted creates the circle using a CSS border with border-radius to make it circular. We can do pretty much the same thing in react-native, though borderRadius in react-native can only be a fixed number and not a percent (edit: this limitation is specific to typescript since the borderRadius property has type number. Percentage strings do work at runtime).

    You can tweak this code however you want, but this will get the job done. You can use IconFA and CircleBorder as two separate nested components but I also made a component IconInCircle which combines the two.

    const IconInCircle = ({ circleSize, borderWidth = 2, borderColor = 'black', ...props}) => (
      <CircleBorder
        size={circleSize}
        borderWidth={borderWidth}
        borderColor={borderColor}
      >
        <IconFA {...props} />
      </CircleBorder>
    );
    
    const CircleBorder = ({ size, borderWidth, borderColor, children }) => (
      <View
        style={{
          width: size,
          height: size,
          borderRadius: 0.5 * size,
          display: 'flex',
          justifyContent: 'center',
          alignItems: 'center',
          borderColor,
          borderWidth,
        }}>
        {children}
      </View>
    );
    

    The IconInCircle component takes three props specific to the border: circleSize, borderWidth, and borderColor. All other props are passed through into the IconFA child component.

    Basically what we are doing is placing the icon inside of a fixed-size View with a circular border and centered contents.

    Now we can use it like so:

          <IconInCircle
            name="plus"
            size={30}
            color="black"
            style={styles.thumbnail}
            borderWidth={1}
            circleSize={50}
          />
    

    Expo Link

    0 讨论(0)
  • 2021-01-28 03:40
    import IconFA from 'react-native-vector-icons/FontAwesome';
    
    <View style={{
      position:'relative',
      justifyContent:'center',
      alignItems:'center',
      width:40,
      height:40,
      backgroundColor:'black'
    }}>
      <IconFA name='circle-thin' size={40} color='grey'/>
      <IconFA name='plus' size={20} color='white' style={{position: 'absolute', zIndex: 99}} />  
    </View>
    

    I am new to ReactNative, but above snippet should work in your case

    Snack Expo

    0 讨论(0)
  • 2021-01-28 03:42

    Try this, just adjust according to your needs, and also don't forget to support other browsers for flex.

    const styles = StyleSheet.create({
      thumbnail: {
        height: 68,
        width: 68,
        position: 'relative',
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center',
        border: '1px solid #333',
        borderRadius: '50%'
      },
    });
    
    0 讨论(0)
提交回复
热议问题