react-native-vector-icons的使用

1.安裝三方包

yarn add react-native-vector-icons或

npm install react-native-vector-icons --save

2.自動(dòng)link

react-native link react-native-vector-icons
ios配置(ios需要單獨(dú)導(dǎo)入字體文件,安卓link完就可以用了)
1.右鍵工程文件Add Files to "(你工程名)"


屏幕快照 2018-02-05 13.49.50.png

2.選擇node_modules/react-native-vector-icons/Fonts文件夾


屏幕快照 2018-02-05 13.56.41.png

3.檢查info.plist文件Fonts provided by application是否成功加入了字體文件


屏幕快照 2018-02-05 13.53.53.png

3.直接使用


import React from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  Platform,
  StatusBar,
} from 'react-native';
import FontAwesome from 'react-native-vector-icons/FontAwesome';

export default class CustomHeader extends React.Component {
  render() {
    return (
     <FontAwesome
            name="angle-left"
            size={26}
            style={{ color: 'white' }}
          />
    );
  }
}


4.自定義icon(ios)(android 直接在 android/app/src/main/assets/fonts文件夾下添加該ttf文件)

1.將自定義icon的svg文件上傳至http://fontello.com/(或其他網(wǎng)站,這里提供我自己使用的),并選中,點(diǎn)擊右上角,download webfont

屏幕快照 2018-02-05 14.01.57.png

2.下載后得到如下
QQ20180205-140436@2x.png

3.將config.json加入到項(xiàng)目文件中,如下代碼中的customIcon.json
4.將font文件夾下ttf文件(此處fontello.ttf), 也通過和上面一樣的Add Files to "(你工程名)"加入到你的項(xiàng)目中(或者直接拖拽到剛才react-native-vector-icons中的那些字體文件的同一目錄下)
QQ20180205-141254@2x.png

5.info.plist文件Fonts provided by application加入了字體文件名


QQ20180205-141600@2x.png

6.依該包的自定義icon引入方法,代碼如下

import React from 'react';
import {
  Text,
  View,
  Button,
  StatusBar,
} from 'react-native';
import { createIconSetFromFontello } from 'react-native-vector-icons';

import clIconConfig from '../config/customIcon.json';
// 自定義icon http://fontello.com/ download webfont config.json 與 ttf文件
// ios中 需要在項(xiàng)目Xcode文件中 Add file to xiangmu, 并在info.plist中的
// fonts provided by application加入改字體文件
// android 需要在 android/app/src/main/assets/fonts文件夾下添加該ttf文件
const CLIcon = createIconSetFromFontello(clIconConfig);

// 導(dǎo)入stack導(dǎo)航組件
export default class HomeScreen extends React.Component {
  render() {
    // 這里可以是導(dǎo)入的其他組件
    const { navigate } = this.props.navigation;
    return (
      <View style={{ backgroundColor: '#fff', flex: 1 }}>
        <Text>Hello, Chat App!</Text>
        <CLIcon
          name="seer-contacts"
          size={26}
          style={{ color: 'red' }}
        />
        <Button
          onPress={() => navigate('Chat')}
          title="Chat with Lucy"
        />
      </View>
    );
  }
}

QQ20180205-141940@2x.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容