在react-native項目中使用iconfont自定義圖標庫
阿新 • • 發佈:2018-05-25
復制 light 自定義 文檔 tabbar lin ext HR sta
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‘ ] //添加你需要賦值的字符文件 ]
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, //等等... }
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
在react-native項目中使用iconfont自定義圖標庫