简体   繁体   English

react native中fontAwesome图标的圆圈轮廓

[英]circle outline for fontAwesome icons in react native

I want to use the fontAwesome + icon such that it is in the middle of a circle.我想使用 fontAwesome + 图标,使其位于圆圈的中间。 I want to use it as one icon item.我想将它用作一个图标项目。 I read that we can use it along with the circle icon and place it inside that but I couldn't make it work.我读到我们可以将它与圆圈图标一起使用并将其放在里面,但我无法让它工作。

import IconFA from 'react-native-vector-icons/FontAwesome';

        <IconFA
         name="plus"
         size={moderateScale(30)}
         color="black"
         style={styles.thumbnail}
         />
        {/* <IconFA
        name="circle-thin"
        size={moderateScale(67)}
        color="white"
      /> */}

  thumbnail: {
    height: 68,
    width: 68,
    position: 'relative',
  },

Alternatively, I read about 'stacked' font awesome icons but couldn't understand how to use it in react native.或者,我阅读了有关“堆叠”字体真棒图标的信息,但无法理解如何在 React Native 中使用它。

Reference: https://jsfiddle.net/1d7fvLy5/1/参考: https : //jsfiddle.net/1d7fvLy5/1/

Snack Expo:小吃展:

https://snack.expo.io/9Ild0Q1zG https://snack.expo.io/9Ild0Q1zG

I want to make something like this:我想做这样的事情:

在此处输入图片说明

I am also open to using a <Thumbnail> if I find a similar icon's link but I couldn't find any such free icon online.如果我找到类似图标的链接,但我在网上找不到任何此类免费图标,我也愿意使用<Thumbnail>

The JSFiddle example that you posted creates the circle using a CSS border with border-radius to make it circular.您发布的 JSFiddle 示例使用带有border-radius的 CSS 边框创建圆形以使其成为圆形。 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).我们可以在 react-native 中做几乎相同的事情,尽管 react-native 中的borderRadius只能是固定数字而不是百分比(编辑:此限制特定于打字稿,因为borderRadius属性具有类型number 。百分比字符串确实有效在运行时)。

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.您可以将IconFACircleBorder用作两个单独的嵌套组件,但我还制作了一个将两者结合起来的组件IconInCircle

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 . IconInCircle组件采用三个特定于边框的道具: circleSizeborderWidthborderColor All other props are passed through into the IconFA child component.所有其他道具都传递到IconFA子组件中。

Basically what we are doing is placing the icon inside of a fixed-size View with a circular border and centered contents.基本上我们正在做的是将图标放置在具有圆形边框和居中内容的固定大小View

Now we can use it like so:现在我们可以像这样使用它:

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

Expo Link世博链接

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我是 ReactNative 的新手,但上面的代码段应该适用于您的情况

Snack Expo小吃展

Try this, just adjust according to your needs, and also don't forget to support other browsers for flex .试试这个,根据自己的需要调整就好,另外别忘了支持其他浏览器的flex

const styles = StyleSheet.create({
  thumbnail: {
    height: 68,
    width: 68,
    position: 'relative',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    border: '1px solid #333',
    borderRadius: '50%'
  },
});

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

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