Categories
JavaScript Answers React Native Answers

How to set absolute positioning horizontal center with React Native?

Sometimes, we want to set absolute positioning horizontal center with React Native.

In this article, we’ll look at how to set absolute positioning horizontal center with React Native.

How to set absolute positioning horizontal center with React Native?

To set absolute positioning horizontal center with React Native, we can set the position and flexbox styles.

For instance, we write:

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

const App = () => {
  return (
    <View
      style={{
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        justifyContent: 'center',
        alignItems: 'center',
      }}>
      <Text>Centered text</Text>
    </View>
  );
};
export default App;

to set the position of the view to 'absolute' to add absolute positioning.

Then we set top, left, right, and bottom to 0 to set the position,

And then we set justifyContent and alignItems to 'center' to center align horizontally and vertically respectively.

Conclusion

To set absolute positioning horizontal center with React Native, we can set the position and flexbox styles.

Categories
JavaScript Answers React Native Answers

How to set responsive font size with React Native?

Sometimes, we want to set responsive font size with React Native.

In this article, we’ll look at how to set responsive font size with React Native.

How to set responsive font size with React Native?

To set responsive font size with React Native, we can use the Dimensions object to get the values to calculate the font size.

For instance, we write:

import * as React from 'react';
import { View, Text, Dimensions, Platform, PixelRatio } from 'react-native';
import { Card } from 'react-native-paper';

const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');

const scale = SCREEN_WIDTH / 320;

const normalize = (size) => {
  const newSize = size * scale;
  if (Platform.OS === 'ios') {
    return Math.round(PixelRatio.roundToNearestPixel(newSize));
  } else {
    return Math.round(PixelRatio.roundToNearestPixel(newSize)) - 2;
  }
};

const App = () => {
  return (
    <View>
      <Text style={{ fontSize: normalize(30) }}>hello</Text>
    </View>
  );
};
export default App;

to define the normalize function that calculates newSize by multipklying size and scale`.

We get the scale by dividing SCREEN_WIDTH by a number.

Then we return the actual font size depending on the platform.

We use PixelRatio.roundToNearestPixel to round to the nearest pixel.

Finally. we set the fontSize of the Text.

Conclusion

To set responsive font size with React Native, we can use the Dimensions object to get the values to calculate the font size.

Categories
JavaScript Answers React Native Answers

How to open a URL in default web browser with React Native?

Sometimes, we want to open a URL in default web browser with React Native.

In this article, we’ll look at how to open a URL in default web browser with React Native.

How to open a URL in default web browser with React Native?

To open a URL in default web browser with React Native, we can use the Linking.openURL method.

For instance, we write:

import * as React from 'react';
import { View, Text, Linking } from 'react-native';

import { Card } from 'react-native-paper';

const App = () => {
  const onPress = async () => {
    const url = 'https://example.com';
    await Linking.canOpenURL(url);
    Linking.openURL(url);
  };

  return (
    <View>
      <Text onPress={onPress}>open</Text>
    </View>
  );
};
export default App;

to define the onPress function that calls Linking.canOpenURL to check if the url can be opened.

If it resolves to true, then we call Linking.openURL with the url to open the link in the default browser of the device.

Conclusion

To open a URL in default web browser with React Native, we can use the Linking.openURL method.

Categories
JavaScript Answers React Native Answers

How to get the screen width in React Native?

Sometimes, we want to get the screen width in React Native.

In this article, we’ll look at how to get the screen width in React Native.

How to get the screen width in React Native?

To get the screen width in React Native, we can use the Dimension object.

For instance, we write:

import * as React from 'react';
import { View, Text, Dimensions } from 'react-native';

import { Card } from 'react-native-paper';

const App = () => {
  const dims = {
    width: Dimensions.get('window').width,
    height: Dimensions.get('window').height,
  };

  return (
    <View>
      <Text>{JSON.stringify(dims)}</Text>
    </View>
  );
};
export default App;

We call Dimensions.get with 'window' to get the screen dimensions in an object.

Then we can use the width and height properties to get the width and height of the screen.

Conclusion

To get the screen width in React Native, we can use the Dimension object.

Categories
JavaScript Answers React Native Answers

How to make text vertical (rotated 90 degrees) in React Native?

Sometimes, we want to make text vertical (rotated 90 degrees) in React Native.

In this article, we’ll look at how to make text vertical (rotated 90 degrees) in React Native.

How to make text vertical (rotated 90 degrees) in React Native?

To make text vertical (rotated 90 degrees) in React Native, we can set the transform style.

For instance, we write:

import * as React from 'react';
import { View, Text } from 'react-native';

import { Card } from 'react-native-paper';

const App = () => {
  return (
    <View>
      <Text
        style={{
          transform: [{ rotate: '90deg' }],
          position: 'fixed',
          top: '30px',
        }}>
        hello world
      </Text>
    </View>
  );
};
export default App;

to set the transform style to [{ rotate: '90deg' }] to rotate the Text component by 90 degrees.

We also set the position to 'fixed' and top to '30px' to move the text.

Now we should see ‘hello world’ displayed vertically.

Conclusion

To make text vertical (rotated 90 degrees) in React Native, we can set the transform style.