最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Input text label on border in react native - Stack Overflow

programmeradmin0浏览0评论

I want to create an input box like this:

But I designed it like this, and I don't know how to add the label on the input border

My Code is:

<InputBox label="Email" icon={true} keyboardType="email-address" defaultValue={emailId} onChangeText={text => setEmailId(text)} />

Input Component:

    import React from 'react';
import {View, StyleSheet, TextInput, Text} from 'react-native';
import {COLORS} from '../constants/colors';
import {FONT_FAMILY} from '../constants/font-family';

export default function InputBox(props) {
  return (
    <View style={styles.container}>
      <View style={{flex: 1}}>
        <Text style={styles.label}>{props.label}</Text>
        <TextInput
          placeholder={props.placeholder}
          placeholderTextColor="#9F9F9F"
          style={styles.input}
          keyboardType={props.keyboardType}
          secureTextEntry={false}
          defaultValue={props.defaultValue}
          onChangeText={props.onChangeText}
          editable={props.editable}
        />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: 20,
    paddingTop: 7.5,
    paddingHorizontal: 12.5,
    paddingBottom: 2.5,
    borderRadius: 5,
    borderWidth: 0.75,
    borderColor: COLORS.WHITE,
  },
  input: {
    fontFamily: FONT_FAMILY.primaryMedium,
    fontSize: 14,
    height: 37,
    color: COLORS.WHITE,
  },
  label: {
    fontFamily: FONT_FAMILY.primary,
    marginLeft: 5,
    color: COLORS.WHITE,
    fontSize: 12,
    //  marginTop: -30,
  },
});

By using react-native-paper, when I add transparent background color it looks like this:

I want to create an input box like this:

But I designed it like this, and I don't know how to add the label on the input border

My Code is:

<InputBox label="Email" icon={true} keyboardType="email-address" defaultValue={emailId} onChangeText={text => setEmailId(text)} />

Input Component:

    import React from 'react';
import {View, StyleSheet, TextInput, Text} from 'react-native';
import {COLORS} from '../constants/colors';
import {FONT_FAMILY} from '../constants/font-family';

export default function InputBox(props) {
  return (
    <View style={styles.container}>
      <View style={{flex: 1}}>
        <Text style={styles.label}>{props.label}</Text>
        <TextInput
          placeholder={props.placeholder}
          placeholderTextColor="#9F9F9F"
          style={styles.input}
          keyboardType={props.keyboardType}
          secureTextEntry={false}
          defaultValue={props.defaultValue}
          onChangeText={props.onChangeText}
          editable={props.editable}
        />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: 20,
    paddingTop: 7.5,
    paddingHorizontal: 12.5,
    paddingBottom: 2.5,
    borderRadius: 5,
    borderWidth: 0.75,
    borderColor: COLORS.WHITE,
  },
  input: {
    fontFamily: FONT_FAMILY.primaryMedium,
    fontSize: 14,
    height: 37,
    color: COLORS.WHITE,
  },
  label: {
    fontFamily: FONT_FAMILY.primary,
    marginLeft: 5,
    color: COLORS.WHITE,
    fontSize: 12,
    //  marginTop: -30,
  },
});

By using react-native-paper, when I add transparent background color it looks like this:

Share Improve this question edited Sep 18, 2021 at 10:20 julien.leroux5 1,5582 gold badges14 silver badges23 bronze badges asked Sep 17, 2021 at 11:37 Juhi KukrejaJuhi Kukreja 1871 gold badge5 silver badges14 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I was able to get this acplished purely in React Native without any dependencies. The trick was to put the Text in a View with a background color the same as the screen's and position the Text absolutely. My example code hardcodes a lot of values, but if you want it to be responsive you'll need to e up with your own way of figuring those values out. The code:

import { View, Text, TextInput, StyleSheet } from "react-native";

const Input = () => {
    return (
        <View>
            <View style={styles.labelContainer}>
                <Text>Email Address</Text>
            </View>
            <View style={styles.inputContainer}>
                <TextInput placeholder="Enter email address" />
            </View>

        </View>
    );
}

const styles = StyleSheet.create({
    labelContainer: {
        backgroundColor: "white", // Same color as background
        alignSelf: "flex-start", // Have View be same width as Text inside
        paddingHorizontal: 3, // Amount of spacing between border and first/last letter
        marginStart: 10, // How far right do you want the label to start
        zIndex: 1, // Label must overlap border
        elevation: 1, // Needed for android
        shadowColor: "white", // Same as background color because elevation: 1 creates a shadow that we don't want
        position: "absolute", // Needed to be able to precisely overlap label with border
        top: -12, // Vertical position of label. Eyeball it to see where label intersects border.
    },
    inputContainer: {
        borderWidth: 1, // Create border
        borderRadius: 8, // Not needed. Just make it look nicer.
        padding: 8, // Also used to make it look nicer
        zIndex: 0, // Ensure border has z-index of 0
    },
});

export default Input;

This is what it looks like on my device (Samsung Galaxy S10+):

If you have to use a material ui styled ponents you may wanna use reactnativepaper

hi there you cna use this code to achive import React from 'react';

import {View, StyleSheet, Text} from 'react-native';

    import { TextInput } from 'react-native-paper';
    export default function InputBox(props) {
      return (
        <View style={styles.container}>
         <TextInput
         mode="outlined"
          label="Email"
          style={{width:'90%'}}
        />
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        justifyContent:'center',alignItems:'center',
     flex:1
        
        // borderColor: COLORS.WHITE,
      },
      input: {
        // fontFamily: FONT_FAMILY.primaryMedium,
        fontSize: 14,
        height: 37,
        // color: COLORS.WHITE,
      },
      label: {
        // fontFamily: FONT_FAMILY.primary,
        marginLeft: 5,
        // color: COLORS.WHITE,
        fontSize: 12,
        //  marginTop: -30,
      },
    });

you can test it on Snack

Also you can read more about props here react-native-paper

发布评论

评论列表(0)

  1. 暂无评论