Categories
JavaScript Answers React Native Answers

How to change TextInput placeHolder alignment with React Native?

Sometimes, we want to change TextInput placeHolder alignment with React Native

In this article, we’ll look at how to change TextInput placeHolder alignment with React Native.

How to change TextInput placeHolder alignment with React Native?

To change TextInput placeHolder alignment with React Native, we can set the textAlign style to 'center'.

For instance, we write:

import * as React from 'react';
import { View, TextInput } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View>
      <TextInput placeholder="Name" style={{ textAlign: 'center' }} />
    </View>
  );
}

to align the placeholder at the center of the screen by setting textAlign set to 'center'.

Conclusion

To change TextInput placeHolder alignment with React Native, we can set the textAlign style to 'center'.

Categories
JavaScript Answers React Native Answers

How to avoid text wrapping with React Native?

Sometimes, we want to avoid text wrapping with React Native.

In this article, we’ll look at how to avoid text wrapping with React Native.

How to avoid text wrapping with React Native?

To avoid text wrapping with React Native, we can set the numberOfLines prop to 1.

For instance, we write:

import * as React from 'react';
import { View, Text } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View style={{ flexDirection: 'row' }}>
      <Text numberOfLines={1} style={{ flex: 1, textAlign: 'left' }}>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      </Text>
    </View>
  );
}

to set the numberOfLines prop of the Text component to 1 to show the text in 1 line and truncate the part that can’t be shown in 1 line.

Conclusion

To avoid text wrapping with React Native, we can set the numberOfLines prop to 1.

Categories
JavaScript Answers React Native Answers

How to upload a photo with Expo?

Sometimes, we want to upload a photo with Expo.

In this article, we’ll look at how to upload a photo with Expo.

How to upload a photo with Expo?

To upload a photo with Expo, we can use the expo-image-picker package.

We install it by running npm i expo-image-picker.

Then we use it by writing:

import * as React from 'react';
import { View, Text } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import * as ImagePicker from 'expo-image-picker';

const takeAndUploadPhotoAsync = async () => {
  let result = await ImagePicker.launchCameraAsync({
    allowsEditing: true,
    aspect: [4, 3],
  });

  if (result.cancelled) {
    return;
  }
  let localUri = result.uri;
  let filename = localUri.split('/').pop();
  let match = /\.(\w+)$/.exec(filename);
  let type = match ? `image/${match[1]}` : `image`;

  let formData = new FormData();
  formData.append('photo', { uri: localUri, name: filename, type });

  return await fetch('https://https://jsonplaceholder.typicode.com/', {
    method: 'POST',
    body: formData,
    headers: {
      'content-type': 'multipart/form-data',
    },
  });
};
export default function App() {
  React.useEffect(() => {
    takeAndUploadPhotoAsync();
  }, []);

  return <View></View>;
}

to define the takeAndUploadPhotoAsync that calls ImagePicker.launchCameraAsync to start the camera.

Then we get the result after the picture is taken with result.uri.

Next, we get the filename with localUri.split('/').pop().

And then we get the MIME type with match ? `image/${match[1]}` : `image`.

Next, we create a new FormData instance and append the photo to that.

And then we call fetch to submit the FormData instance.

Conclusion

To upload a photo with Expo, we can use the expo-image-picker package.

We install it by running npm i expo-image-picker.

Categories
JavaScript Answers React Native Answers

How to adjust font size to fit view in React Native for Android?

Sometimes, we want to adjust font size to fit view in React Native for Android.

In this article, we’ll look at how to adjust font size to fit view in React Native for Android.

How to adjust font size to fit view in React Native for Android?

To adjust font size to fit view in React Native for Android, we can adjust the font size to fit the text onto the number of lines.

For instance, we write:

import * as React from 'react';
import { View, Text } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

const AdjustLabel = ({ fontSize, text, style, numberOfLines }) => {
  const [currentFont, setCurrentFont] = React.useState(fontSize);

  return (
    <Text
      numberOfLines={numberOfLines}
      adjustsFontSizeToFit
      style={[style, { fontSize: currentFont }]}
      onTextLayout={(e) => {
        const { lines } = e.nativeEvent;
        if (lines.length > numberOfLines) {
          setCurrentFont(currentFont - 1);
        }
      }}>
      {text}
    </Text>
  );
};

export default function App() {
  return (
    <View style={{ overflow: 'hidden', paddingBottom: 5 }}>
      <AdjustLabel
        text="Lorem ipsum dolor sit amet, consectetur adipiscing elit"
        fontSize={50}
        numberOfLines={2}
      />
    </View>
  );
}

to create the AdjustLabel component that has the adjustsFontSizeToFit prop and also calculates the font size in the onTextLayout callback.

We calculate the font size in the callback by getting lines.length from the event.

Then we adjust the font if lines.length is bigger than numberOfLines.

And we set the fontSize to currentFont to change the font size.

Conclusion

To adjust font size to fit view in React Native for Android, we can adjust the font size to fit the text onto the number of lines.

Categories
JavaScript Answers React Native Answers

How to set elevation shadow only on the bottom on React Native?

Sometimes, we want to set elevation shadow only on the bottom on React Native.

In this article, we’ll look at how to set elevation shadow only on the bottom on React Native.

How to set elevation shadow only on the bottom on React Native?

To set elevation shadow only on the bottom on React Native, we wrap out View with another View and set its overflow to 'hidden'.

For instance, we write:

import * as React from 'react';
import { View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View style={{ overflow: 'hidden', paddingBottom: 5 }}>
      <View
        style={{
          backgroundColor: '#fff',
          width: 300,
          height: 60,
          shadowColor: '#000',
          shadowOffset: { width: 1, height: 1 },
          shadowOpacity: 0.4,
          shadowRadius: 3,
          elevation: 5,
        }}
      />
    </View>
  );
}

to set overflow to 'hidden' on the outer View.

And then we add the shadow styles in the inner view to add the shadow.

elevation is needed for Android to show the shadow.

Conclusion

To set elevation shadow only on the bottom on React Native, we wrap out View with another View and set its overflow to 'hidden'.