|
| 1 | +<h1>Make your React-Native app sexy on every size</h1> |
| 2 | + |
| 3 | +Did it ever happen to you that your designer handed you a cool design, |
| 4 | +you developed it on, let's say, an iPhone 7, |
| 5 | +and when you run it on an a Tablet everything looked like you left it too long at the dryer? |
| 6 | +<br/> |
| 7 | +That's probably because the design was created using dp (density pixels). |
| 8 | +To keep things simple - the bigger your device the more dp it'll have. |
| 9 | +<br/>When working with React-Native iPhone 7 has **375dp** width and **667dp** height and a Galaxy Tab A 8.0" Tablet (the one I'm using) |
| 10 | +has **768dp** width and **1024dp** height.<br/> |
| 11 | +So while a `<View style={{width: 300, height: 450}}/>` will cover most of your iPhone screen |
| 12 | +it will cover less than half of your tablet screen. |
| 13 | + |
| 14 | +<h4>So how can I make my app sexy on the tablet as well?</h4> |
| 15 | +Oh! I'm glad you asked. On this blog post I'll show several methods to scale your components among |
| 16 | +different screen sizes, and which one I found best. |
| 17 | +For this purpose I created a small example app, and on every scaling method I'll attach the component's |
| 18 | +code along with a screenshot an a tablet and an iPhone. |
| 19 | + |
| 20 | +<h4>No scaling</h4> |
| 21 | + |
| 22 | +So this is the component: |
| 23 | +```javascript |
| 24 | +import React from 'react'; |
| 25 | +import { View, Text, Dimensions, StyleSheet, TouchableOpacity } from 'react-native'; |
| 26 | +import { loremIpsum } from './contants'; |
| 27 | +const { width, height } = Dimensions.get('window'); |
| 28 | + |
| 29 | +const AwesomeComponent = () => |
| 30 | + <View style={styles.container}> |
| 31 | + <View style={styles.box}> |
| 32 | + <Text style={styles.title}>Awesome Blog Post Page</Text> |
| 33 | + <Text style={styles.text}>{loremIpsum + '\nwidth: ' + width + '\nheight: ' + height}</Text> |
| 34 | + <View style={styles.buttonsContainer}> |
| 35 | + <TouchableOpacity style={styles.button}> |
| 36 | + <Text style={styles.buttonText}>Accept</Text> |
| 37 | + </TouchableOpacity> |
| 38 | + <TouchableOpacity style={styles.button}> |
| 39 | + <Text style={styles.buttonText}>Decline</Text> |
| 40 | + </TouchableOpacity> |
| 41 | + </View> |
| 42 | + </View> |
| 43 | + </View>; |
| 44 | + |
| 45 | +export default AwesomeComponent; |
| 46 | +``` |
| 47 | + |
| 48 | +And this is the StyleSheet: |
| 49 | +```javascript |
| 50 | +const styles = StyleSheet.create({ |
| 51 | + container: { |
| 52 | + width: width, |
| 53 | + height: height, |
| 54 | + backgroundColor: '#F0D6FF', |
| 55 | + alignItems: 'center', |
| 56 | + justifyContent: 'center', |
| 57 | + }, |
| 58 | + box: { |
| 59 | + width: 300, |
| 60 | + height: 450, |
| 61 | + backgroundColor: 'white', |
| 62 | + borderRadius: 10, |
| 63 | + padding: 10, |
| 64 | + shadowColor: 'black', |
| 65 | + shadowOpacity: 0.5, |
| 66 | + shadowRadius: 3, |
| 67 | + shadowOffset: { |
| 68 | + height: 0, |
| 69 | + width: 0 |
| 70 | + }, |
| 71 | + elevation: 2 |
| 72 | + }, |
| 73 | + title: { |
| 74 | + fontSize: 20, |
| 75 | + fontWeight: 'bold', |
| 76 | + marginBottom: 10 |
| 77 | + }, |
| 78 | + text: { |
| 79 | + fontSize: 14 |
| 80 | + }, |
| 81 | + buttonsContainer: { |
| 82 | + flex: 1, |
| 83 | + justifyContent: 'flex-end', |
| 84 | + alignItems: 'center' |
| 85 | + }, |
| 86 | + button: { |
| 87 | + width: 150, |
| 88 | + height: 45, |
| 89 | + borderRadius: 100, |
| 90 | + marginBottom: 10, |
| 91 | + backgroundColor: '#66E8FF', |
| 92 | + alignItems: 'center', |
| 93 | + justifyContent: 'center', |
| 94 | + }, |
| 95 | + buttonText: { |
| 96 | + fontWeight: 'bold', |
| 97 | + fontSize: 14 |
| 98 | + } |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +As you can see all my StyleSheet sizes are in dp units on no scaling was done. |
| 103 | +This will end up looking like this: |
| 104 | + |
0 commit comments