5

In my application, I want to have large paragraphs of text with a timestamp center aligned with the first line of each Text object. Is there a way to do this?

Here is an example of what I'm trying to do. https://i.stack.imgur.com/TXCN6.png

I've tried isolating the first line into a separate object, but I couldn't figure out how to. In CSS I could have used pseudo-elements to get this done.

My current code for the two elements looks something like

<View>
    <Text style={styles.timestamp}>6:42 AM</Text>
    <Text style={styles.paragraph}>
        A long paragraph of text for demonstration.
    </Text>
</View>
Willie Jeng
  • 323
  • 1
  • 4
  • 9

2 Answers2

0

Take a look at this:

import React, { Component } from "react";
import { View, Text, ScrollView } from "react-native";

class Test extends Component {
  render() {
    return (
      <View
        style={{
          flex: 1,
          justifyContent: "center",
          alignItems: "center"
        }}
      >
        <View
          style={{
            flex: 1,
            flexDirection: "row",
            justifyContent: "center",
            alignItems: "center",
            alignSelf: "stretch",
            margin: 30,
            borderWidth: 1
          }}
        >
          {/* timestamp */}
          <View
            style={{
              flex: 1,
              justifyContent: "flex-start",
              alignItems: "center",
              alignSelf: "stretch"
            }}
          >
            <Text style={{ backgroundColor: "lime", fontSize: 22 }}>
              6:42 AM
            </Text>
          </View>

          {/* text */}
          <View
            style={{
              flex: 2.5,
              justifyContent: "center",
              alignItems: "center",
              alignSelf: "stretch"
            }}
          >
            <ScrollView style={{ flex: 1, alignSelf: "stretch" }}>
              <Text style={{ fontSize: 22 }}>
                <Text style={{ backgroundColor: "cyan", fontSize: 22 }}>
                  Line with color{"\n"}
                </Text>
                Lorem Ipsum is simply dummy text of the printing and typesetting
                industry. Lorem Ipsum has been the industry's standard dummy
                text ever since the 1500s, when an unknown printer took a galley
                of type and scrambled it to make a type specimen book. It has
                survived not only five centuries, but also the leap into
                electronic typesetting, remaining essentially unchanged. It was
                popularised in the 1960s with the release of Letraset sheets
                containing Lorem Ipsum passages, and more recently with desktop
                publishing software like Aldus PageMaker including versions of
                Lorem Ipsum. The standard chunk of Lorem Ipsum used since the
                1500s is reproduced below for those interested. Sections 1.10.32
                and 1.10.33 from de Finibus Bonorum et Malorum by Cicero are
                also reproduced in their exact original form, accompanied by
                English versions from the 1914 translation by H. Rackham. Lorem
                Ipsum is simply dummy text of the printing and typesetting
                industry. Lorem Ipsum has been the industry's standard dummy
                text ever since the 1500s, when an unknown printer took a galley
                of type and scrambled it to make a type specimen book. It has
                survived not only five centuries, but also the leap into
                electronic typesetting, remaining essentially unchanged. It was
                popularised in the 1960s with the release of Letraset sheets
                containing Lorem Ipsum passages, and more recently with desktop
                publishing software like Aldus PageMaker including versions of
                Lorem Ipsum. The standard chunk of Lorem Ipsum used since the
                1500s is reproduced below for those interested. Sections 1.10.32
                and 1.10.33 from de Finibus Bonorum et Malorum by Cicero are
                also reproduced in their exact original form, accompanied by
                English versions from the 1914 translation by H. Rackham.
              </Text>
            </ScrollView>
          </View>
        </View>
      </View>
    );
  }
}

export default Test;

If you need the timestamp to scroll too, let me know!

MohamadKh75
  • 2,582
  • 5
  • 28
  • 54
0

It's that easy

<View style={{ flexDirection: 'row' }}>
    <Text>6:42 AM</Text>
    <Text>       </Text> // this is just for separating them, you can do it in better ways
    <Text>{text}</Text>
</View>

because they are rendered in row direction, you'll get what you want. you know that you must replace that hardcoded time with you object and etc... enter image description here

Amir Hedieh
  • 1,120
  • 1
  • 14
  • 28