0

When attempting to use ScrollView it appears to not respect justifyContent of its parent container.

import React from 'react';
import { Text, ScrollView, StyleSheet, TextStyle, View, ViewStyle } from 'react-native';

interface TODO_TextCard {
  text: string,
}

export const TODO_TextCard: React.FunctionComponent<TODO_TextCard> = (props: TODO_TextCard) => {

  return <View style={styles.viewStyle}>
    <ScrollView>
      <Text style={styles.quoteTextStyle}>{props.text}</Text>
    </ScrollView>
  </View>;
}
const styles = StyleSheet.create({
  quoteTextStyle: {
    fontSize: 30,
    fontStyle: 'italic'
  } as TextStyle,
  viewStyle: {
    flex: 1,
    borderWidth: 2, borderColor: 'red',
    justifyContent: 'center',
    paddingHorizontal: 10
  } as ViewStyle,
});

      <TODO_TextCard text={'The mind adapts and converts to its own purposes the obstacle to our acting. The impediment to action advances action. What stands in the way becomes the way'}/>

Rendered as:

enter image description here

Now if I remove the and just render text such as

export const TODO_TextCard: React.FunctionComponent<TODO_TextCard> = (props: TODO_TextCard) => {

  return <View style={styles.viewStyle}>
      <Text style={styles.quoteTextStyle}>{props.text}</Text>
  </View>;
}

The Text element does respect the justifyContent:center of the parent and renders as:

enter image description here

Is it possible for Scroll view to be centered?

The solution that I have in mind right now is to check the length of the text and conditionally render Scroll View something like:


/** This some text length would have to be different depending on the device screen, and
 *  even with different device screens would still not work all the time if the text
 *  can have new lines in it.*/
const SOME_TEXT_LENGTH = 300;
export const TODO_TextCard: React.FunctionComponent<TODO_TextCard> = (props: TODO_TextCard) => {

  return <View style={styles.viewStyle}>

    {props.text.length > SOME_TEXT_LENGTH ?
      <ScrollView>
        <Text style={styles.quoteTextStyle}>{props.text}</Text>
      </ScrollView>
      :
      <Text style={styles.quoteTextStyle}>{props.text}</Text>
    }
  </View>;
}

Which very much is not ideal, due to different device screens as well as text potentially having new lines.

user7858768
  • 838
  • 7
  • 22

2 Answers2

0

The ScrollView does in fact respect the justifyContent:center of its parent. The ScrollView is placed in the center of the outer View component. But here the ScrollView takes up the whole vertical screen, so it seems like it is not centered.

Try setting <ScrollView style={{backgroundColor: 'green'}}> to see what i mean.

enter image description here

Try applying viewStyle or a similar styling to the ScrollView itself. Make sure that you use the attribute contentContainerStyle instead of style. This snippet works for me.

<View style={styles.viewStyle}>
  <ScrollView contentContainerStyle={{flex: 1, justifyContent: 'center'}}>
    <Text style={styles.quoteTextStyle}>{props.text}</Text>
  </ScrollView>
</View>

enter image description here

I also found this article. Maybe it will also help you out.

PiaBa
  • 155
  • 5
0

You need to give styling to ScrollView, for styling ScrollView you can use style or contentContainerStyle prop:

style defines the outer container of the ScrollView, e.g its height and relations to siblings elements

contentContainerStyle defines the inner container of it, e.g items alignments, padding, etc

In your case you need to give contentContainerStyle to position your items for eg:

return (
   <View style={styles.viewStyle}>
    {props.text.length > SOME_TEXT_LENGTH ?
      <ScrollView 
          contentContainerStyle={{
                  flex: 1,  //To take full screen height
                  justifyContent: 'center',
                  alignItems: 'center,
     }}>
        <Text style={styles.quoteTextStyle}>{props.text}</Text>
      </ScrollView>
      :
      <Text style={styles.quoteTextStyle}>{props.text}</Text>
    }
  </View>
);
Rahman Haroon
  • 1,088
  • 2
  • 12
  • 36