How can I put an icon inside a TextInput in React Native?

前端 未结 9 2088
轻奢々
轻奢々 2020-12-04 19:03

I am thinking of having something like this https://android-arsenal.com/details/1/3941 where you have icon that you press to show password as plaintext, not as dots. However

相关标签:
9条回答
  • 2020-12-04 19:36

    You can wrap your TextInput in View.

    <View>
      <TextInput/>
      <Icon/>
    <View>

    and dynamically calculate width, if you want add an icon,

    iconWidth = 0.05*viewWidth 
    textInputWidth = 0.95*viewWidth
    

    otherwise textInputwWidth = viewWidth.

    View and TextInput background color are both white. (Small hack)

    0 讨论(0)
  • 2020-12-04 19:36
    //This is an example code to show Image Icon in TextInput// 
    import React, { Component } from 'react';
    //import react in our code.
    
    import { StyleSheet, View, TextInput, Image } from 'react-native';
    //import all the components we are going to use. 
    
    export default class App extends Component<{}> {
      render() {
        return (
          <View style={styles.container}>
            <View style={styles.SectionStyle}>
              <Image
                //We are showing the Image from online
                source={{uri:'http://aboutreact.com/wp-content/uploads/2018/08/user.png',}}
    
                //You can also show the image from you project directory like below
                //source={require('./Images/user.png')}
    
                //Image Style
                style={styles.ImageStyle}
              />
    
              <TextInput
                style={{ flex: 1 }}
                placeholder="Enter Your Name Here"
                underlineColorAndroid="transparent"
              />
            </View>
             <View style={styles.SectionStyle}>
              <Image
                //We are showing the Image from online
                source={{uri:'http://aboutreact.com/wp-content/uploads/2018/08/phone.png',}}
    
                //You can also show the image from you project directory like below
                //source={require('./Images/phone.png')}
    
                //Image Style
                style={styles.ImageStyle}
              />
    
              <TextInput
                style={{ flex: 1 }}
                placeholder="Enter Your Mobile No 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: 0.5,
        borderColor: '#000',
        height: 40,
        borderRadius: 5,
        margin: 10,
      },
    
      ImageStyle: {
        padding: 10,
        margin: 5,
        height: 25,
        width: 25,
        resizeMode: 'stretch',
        alignItems: 'center',
      },
    });
    

    Expo

    0 讨论(0)
  • 2020-12-04 19:38

    Basically you can’t put an icon inside of a textInput but you can fake it by wrapping it inside a view and setting up some simple styling rules.

    Here's how it works:

    • put both Icon and TextInput inside a parent View
    • set flexDirection of the parent to ‘row’ which will align the children next to each other
    • give TextInput flex 1 so it takes the full width of the parent View
    • give parent View a borderBottomWidth and push this border down with paddingBottom (this will make it appear like a regular textInput with a borderBottom)
      • (or you can add any other style depending on how you want it to look)

    Code:

    <View style={styles.passwordContainer}>
      <TextInput
        style={styles.inputStyle}
          autoCorrect={false}
          secureTextEntry
          placeholder="Password"
          value={this.state.password}
          onChangeText={this.onPasswordEntry}
        />
      <Icon
        name='what_ever_icon_you_want'
        color='#000'
        size={14}
      />
    </View>
    

    Style:

    passwordContainer: {
      flexDirection: 'row',
      borderBottomWidth: 1,
      borderColor: '#000',
      paddingBottom: 10,
    },
    inputStyle: {
      flex: 1,
    },
    

    (Note: the icon is underneath the TextInput so it appears on the far right, if it was above TextInput it would appear on the left.)

    0 讨论(0)
提交回复
热议问题