چطور در react native از آیکون ها استفاده کنیم؟
- نفیسه افقی 1 سال قبل سوال کرد
- شما باید برای ارسال دیدگاه وارد شوید
یکی از محبوب ترین کتابخانه ها برای استفاده از آیکون در react native ، کتابخانه react native vector icons
است. اول بصورت زیر نصبش کنید:
npm install react-native-vector-icons --save
بعد کافی است تا دسته آیکون هایی که می خواهید استفاده کنید را از یک فونت خاص ایمپورت کنید (مثلا: FontAwesome
) . حالا بصورت زیر می توانید از آن ها استفاده کنید:
// Example to Use React Native Vector Icons
// https://aboutreact.com/react-native-vector-icons/
// Import React
import React from 'react';
// Import required component
import {SafeAreaView, StyleSheet, Text, View} from 'react-native';
// Import vector icons
import Icon from 'react-native-vector-icons/FontAwesome';
const App = () => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.heading}>
Example to Use React Native Vector Icons
</Text>
<View style={styles.iconContainer}>
<Text>
<Icon name="rocket" size={30} color="#900" />
</Text>
{/* Icon Component */}
<Icon name="rocket" size={30} color="#900" />
</View>
<View style={{marginTop: 16, marginBottom: 16}}>
{/* Icon.Button Component */}
<Icon.Button
name="facebook"
backgroundColor="#3b5998"
onPress={() => alert('Login with Facebook')}>
Login with Facebook
</Icon.Button>
</View>
</View>
<Text style={styles.footerTitle}>Vector Icons</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
heading: {
fontSize: 20,
textAlign: 'center',
marginBottom: 20,
},
iconContainer: {
marginTop: 16,
marginBottom: 16,
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center',
},
footerTitle: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
});
export default App;
*به دو صورت زیر می توانید از icon ها استفاده کنید:
<Icon.Button
name="facebook"
backgroundColor="#3b5998"
onPress={() => alert('Login with Facebook')}>
Login with Facebook
</Icon.Button>
<Icon name="rocket" size={30} color="#900" />
*اگر روی گوشی های اندروید آیکونی نمایش داده نشد، این کار را بکنید: داخل فایل build.gradle در پوشه android/build ، خط زیر را به انتهای فایل اضافه کنید و مجدد بیلد بگیرید (npm run android):
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
*react native vector icons چندین بسته معروف از آیکن ها را ساپورت می کند. لینک آیکن ها و اسامی شان را ببینید:
Ionicons
Octicons
Foundation Icons
Entypo
Zocial
Evil Icons
*اگر از این پکیج ها استفاده می کنید باید هر کدام را جدا ایمپورت و استفاده کنید:
import SimpleLineIcons from 'react-native-vector-icons/SimpleLineIcons';
// Use an icon in your component
<SimpleLineIcons name="user" size={30} color="black" />;
- نفیسه افقی 1 سال قبل پاسخ داد
- آخرین ویرایش 1 سال قبل
- شما باید برای ارسال دیدگاه وارد شوید