gpt4 book ai didi

react-native - 如何在 React Native 中按第一行对齐文本对象

转载 作者:行者123 更新时间:2023-12-04 13:58:01 25 4
gpt4 key购买 nike

在我的应用程序中,我希望有大段文本,其时间戳中心与每个 Text 对象的第一行对齐。有没有办法做到这一点?

这是我正在尝试做的一个例子。 https://i.imgur.com/VJzK4V2.png

我试过将第一行隔离到一个单独的对象中,但我不知道该怎么做。在 CSS 中,我可以使用伪元素来完成这项工作。

我当前的两个元素代码看起来像

<View>
<Text style={styles.timestamp}>6:42 AM</Text>
<Text style={styles.paragraph}>
A long paragraph of text for demonstration.
</Text>
</View>

最佳答案

看看这个:

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;

如果您也需要滚动时间戳,请告诉我!

关于react-native - 如何在 React Native 中按第一行对齐文本对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com