【问题标题】:circle outline for fontAwesome icons in react nativereact native 中 fontAwesome 图标的圆形轮廓
【发布时间】:2021-01-23 23:07:19
【问题描述】:

我想使用 fontAwesome + 图标,使其位于圆圈的中间。我想将它用作一个图标项目。我读到我们可以将它与圆形图标一起使用并将其放在其中,但我无法使其工作。

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',
  },

另外,我读到了“堆叠”字体很棒的图标,但不明白如何在 react native 中使用它。

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

小吃博览会:

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

我想做这样的事情:

如果我找到类似图标的链接,我也愿意使用&lt;Thumbnail&gt;,但我在网上找不到任何此类免费图标。

【问题讨论】:

  • 在您的参考示例中,圆圈实际上并不是一个图标。这是一个带有边界半径的css边界,使其成为圆形。这种区别对你来说重要吗?
  • 要完成之前的评论,请尝试const styles = StyleSheet.create({ thumbnail: { height: 68, width: 68, position: 'relative', border: '1px solid', borderRadius:'50%', display:'inline-flex', justifyContent:'center', alignItems:'center' }, } 为您的风格。
  • @G-Cyrillus 这不适用于 react native,因为边框不是样式表中的样式属性。此外,不能使用 inline-flex 。但除此之外,是的,我也不介意为此使用纯 CSS。
  • 哎呀,我信任:snack.expo.io/qz_1J!ZGR 看起来工作正常;)

标签: javascript css reactjs typescript react-native


【解决方案1】:
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>

我是 ReactNative 的新手,但上面的 sn-p 应该适用于你的情况

Snack Expo

【讨论】:

    【解决方案2】:

    试试这个,根据自己的需要调整,也别忘了支持flex的其他浏览器。

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

    【讨论】:

    • 这不适用于 react native,因为边框不是样式表中的样式属性。此外,我们不能对borderRadius 使用字符串值。
    【解决方案3】:

    您发布的 JSFiddle 示例使用带有 border-radius 的 CSS 边框创建了圆形,以使其成为圆形。我们可以在 react-native 中做几乎相同的事情,尽管 react-native 中的 borderRadius 只能是固定数字而不是百分比(编辑:此限制特定于 typescript,因为 borderRadius 属性的类型为 @987654325 @. 百分比字符串在运行时有效)。

    您可以根据需要调整此代码,但这将完成工作。您可以将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>
    );
    

    IconInCircle 组件采用三个特定于边框的道具:circleSizeborderWidthborderColor。所有其他 props 都传递到 IconFA 子组件中。

    基本上我们所做的是将图标放在固定大小的View 内,带有圆形边框和居中的内容。

    现在我们可以这样使用它了:

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

    Expo Link

    【讨论】:

    • 有没有什么办法可以使用一些填充/边框来减少加号的厚度?
    猜你喜欢
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2019-01-26
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多