在react-native项目中使用iconfont自定义图标库(android)

 

1. 安装react-native-vector-icons

yarn add react-native-vector-icons
react-native link

如果没有关联成功的话,可以参考官方文档手动配置一下

2. 将从阿里下载的iconfont.tff文件复制到以下目录

3. 在android/app/build.gradle中添加:

project.ext.vectoricons = [
    iconFontNames: ['iconfont.ttf' ] //添加你需要赋值的字符文件
]
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

4. 在项目中创建字体配置文件

// DIYIcon.js

import createIconSet from 'react-native-vector-icons/lib/create-icon-set';
import glyphMap from './iconfont.json';

const iconSet = createIconSet(glyphMap, 'DIYIcon', 'iconfont.ttf');   //  'DIYIcon'可自定义名称

export default iconSet;

export const Button = iconSet.Button;
export const TabBarItem = iconSet.TabBarItem;
export const TabBarItemIOS = iconSet.TabBarItemIOS;
export const ToolbarAndroid = iconSet.ToolbarAndroid;
export const getImageSource = iconSet.getImageSource;

  

// iconfont.json

{
  "glass": 61440,
  "music": 61441,
  "search": 61442,
  "envelope-o": 61443,
  "heart": 61444,
  "star": 61445,
  "star-o": 61446,
  "user": 61447,
  //等等...
}

 

附iconfont.json自动提取的方法:

4.1 安装python

点击下载pthon安装包https://www.python.org/ftp/python/2.7.14/python-2.7.14.msi
等待安装完成!

4.2 配置python环境变量

在path中添加 python安装目录以及Scripts 

 

4.3 安装 fonttools

pip install fonttools

 

具体介绍请参考:github地址

4.4 准备react-native-iconfont-mapper

直接打包下载react-native-iconfont-mapper,或者通过git克隆到本地,这个目录自己选个容易记住的,因为以后用的比较多。

 
项目比较单一,仅仅一个python文件。

4.5 提取字符表

将前面下载的字体包中的ttf文件拷贝到这里 

 
python iconfont-mapper.py iconfont.ttf iconfont.js

 

 
 

不出意外,可以生成一个iconfont.js文件,打开查看便可以看到我们所需要的json字符串

 
 

4.6 新建iconfont.json

{
  "home": 58880,
  "setting": 58881,
  "code": 58886,
  "money": 58951,
  "phone": 58952,
  "user": 58890,
  "customer": 58993,
  "message": 59034,
  "add": 59418,
  "password": 58910
}

 

这里需要注意,刚才生成的字符表json对象后面的值有引号,这里需要删除



5. 使用

import DIYIcon from './diyicon/DIYIcon';

...

<DIYIcon name={'glass'} size={'50'} color={'#ed4040'} />

 

 

 

参考:

http://www.imbeta.cn/zai-react-nativezhong-you-ya-de-shi-yong-iconfont.html

https://segmentfault.com/a/1190000009939727

https://www.jianshu.com/p/332198bf46a7

 

转载于:https://www.cnblogs.com/ImaY/p/9090311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
react-native-reanimated 是一个 React Native 库,用于实现高性能、流畅的动画和交互效果。在 Android react-native-reanimated 的使用方式与在 iOS 基本相同。以下是 react-native-reanimated 在 Android 的用法: 1. 安装 react-native-reanimated 在项目根目录运行以下命令: ``` npm install react-native-reanimated ``` 或者使用 yarn: ``` yarn add react-native-reanimated ``` 2. 连接 react-native-reanimated 在项目根目录运行以下命令: ``` react-native link react-native-reanimated ``` 3. 在代码使用 在需要使用 react-native-reanimated 的组件或页面,首先导入库: ```javascript import Animated from 'react-native-reanimated'; ``` 然后就可以使用 Animated 提供的各种动画和交互效果了。例如,以下代码实现了一个在点击时平移的按钮: ```javascript import React, { useState } from 'react'; import { View, TouchableOpacity, StyleSheet } from 'react-native'; import Animated, { useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated'; const App = () => { const [isPressed, setIsPressed] = useState(false); const translateX = useSharedValue(0); const handlePressIn = () => { setIsPressed(true); translateX.value = withSpring(-50); }; const handlePressOut = () => { setIsPressed(false); translateX.value = withSpring(0); }; const animatedStyle = useAnimatedStyle(() => { return { transform: [{ translateX: translateX.value }], }; }); return ( <View style={styles.container}> <TouchableOpacity onPressIn={handlePressIn} onPressOut={handlePressOut} activeOpacity={0.8} > <Animated.View style={[styles.button, animatedStyle]}> <Text style={styles.text}>{isPressed ? 'Pressed' : 'Press me'}</Text> </Animated.View> </TouchableOpacity> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, button: { width: 120, height: 40, borderRadius: 20, backgroundColor: 'blue', alignItems: 'center', justifyContent: 'center', }, text: { color: 'white', fontWeight: 'bold', }, }); export default App; ``` 在这个例子,我们使用了 useSharedValue 创建了一个共享变量 translateX,表示按钮在 x 轴方向的平移量。在 handlePressIn 和 handlePressOut ,我们分别更新了按钮的状态和 translateX 的值,使用 withSpring 让按钮平滑地过渡到新的位置。最后,我们使用 useAnimatedStyle 创建了一个动画样式 animatedStyle,并将其应用到了按钮的样式。 以上就是 react-native-reanimated 在 Android 的基本用法。要了解更多关于 react-native-reanimated 的内容,请参考官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值