اضافه کردن icon درون TextInput در react native


سلام دوستان.دراین آموزش متنی قصد داریم یاد بگیریم که چجوری یک icon درون TextInput اضافه کنیم.TextInput با icon میتونه خیلی خوشکلتر باشه.در اکثر اپلیکیشن های جدید و پیشرفته ReactNative توسعه دهنده ها از این نوع فرم Login استفاده میکنند .پس با من همراه باشید تا قدم به قدم یک icon به TextInput اضافه کنیم.میتونید برای دانلود رایگان icon از Google Material Design Icons استفاده کنید.
1.ایجاد کردن یک پوشه به نام Images درون root پروژه و قرار دادن icon درون این پوشه.
2.اضافه کردن کامپوننت های StyleSheet, View, TextInput و Image درون بلاک import
1 2 3 |
import React, { Component } from 'react'; import { StyleSheet, View, TextInput, Image} from 'react-native'; |
3.اضافه کردن کامپوننت View در بلاک render’s return.
1 2 3 4 5 6 7 8 9 |
render() { return ( <View style={styles.container}> </View> ); } |
4.ایجاد یک View دیگه درون View اصلی.ادرون این View کامپوننت های Image + TextInput قرار میگیرد.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
render() { return ( <View style={styles.container}> <View style={styles.SectionStyle}> </View> </View> ); } |
5.اضافه کردن کامپوننت Image درون View با استایل SectionStyle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
render() { return ( <View style={styles.container}> <View style={styles.SectionStyle}> <Image source={require('./Images/ic_person.png')} style={styles.ImageStyle} /> </View> </View> ); } |
6.اضافه کردن کامپوننت TextInput بعد از کامپوننت Image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
render() { return ( <View style={styles.container}> <View style={styles.SectionStyle}> <Image source={require('./Images/ic_person.png')} style={styles.ImageStyle} /> <TextInput style={{flex:1}} placeholder="Enter Your Email Here" underlineColorAndroid="transparent" /> </View> </View> ); } |
7.ایجاد استایل سفارشی
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', margin: 10 }, SectionStyle: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', borderWidth: .5, borderColor: '#000', height: 40, borderRadius: 5 , margin: 10 }, ImageStyle: { padding: 10, margin: 5, height: 25, width: 25, resizeMode : 'stretch', alignItems: 'center' }, }); |
8.کد کامل برنامه در فایل App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import React, { Component } from 'react'; import { StyleSheet, View, TextInput, Image} from 'react-native'; export default class App extends Component<{}> { render() { return ( <View style={styles.container}> <View style={styles.SectionStyle}> <Image source={require('./Images/ic_person.png')} style={styles.ImageStyle} /> <TextInput style={{flex:1}} placeholder="Enter Your Email Here" underlineColorAndroid="transparent" /> </View> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', margin: 10 }, SectionStyle: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', borderWidth: .5, borderColor: '#000', height: 40, borderRadius: 5 , margin: 10 }, ImageStyle: { padding: 10, margin: 5, height: 25, width: 25, resizeMode : 'stretch', alignItems: 'center' }, }); |
اسکرین شات در اپلیکیشن Android:
اسکرین شات در اپلیکیشن Ios:
دیدگاهتان را بنویسید