gpt4 book ai didi

flexbox - 文本组件的样式部分

转载 作者:行者123 更新时间:2023-12-02 00:56:48 25 4
gpt4 key购买 nike

我正在尝试制作 React 原生组件,其中同一组件中的不同单词具有不同的样式。

例如,我的按钮上有名称和副标题,我希望副标题文本更小。

<Text style={styles.buttonText}>{speciesArry[i].title} {"\n"}       {speciesArry[i].subtitle}</Text>

我需要将字幕包装在它自己的组件中以便我可以设置它的样式吗?

第二个例子

我有另一个带有文本组件的按钮 绿色 - 最佳选择我需要“绿色”这个词是绿色的。

最佳答案

对于第一个问题,一个带有标题和副标题的按钮,您可以将 View 包装在 TouchableHighlight 中,然后放置多个 Text 元素来创建标题和副标题:

<TouchableHighlight style={{height:70, backgroundColor: 'blue'}}>
<View style={{flexDirection: 'column', }}>
<Text style={{textAlign:'center', color: 'white', fontSize:22, marginTop:10}}>MAIN TEXT</Text>
<Text style={{textAlign:'center', color: 'white', fontSize:14}}>Subtitle</Text>
</View>
</TouchableHighlight>

对于第二个问题,在单个文本组件中具有不同的样式,您需要将文本组件包装在文本组件中,就像您在 html 中使用 span 一样:

<Text style={styles.mainStyle}>This text is red, <Text style={styles.blacktext}>this text is black, </Text><Text style={styles.boldgreen}>this text is bold and green!</Text></Text>

您上面提供的按钮示例看起来像这样:

<TouchableHighlight style={{height:70, backgroundColor: 'orange'}}>
<View style={{ flexDirection: 'row', justifyContent:'center'}}>
<Text style={{fontSize:18, marginTop: 22}}><Text style={{color: 'green'}}>Green</Text> - best Choice</Text>
</View>
</TouchableHighlight>

对于 float:right 的问题:在 flexbox 中没有完全相似的东西,但是有一个非常相似的东西叫做:justifyContent: 'flex-end'。下面是它的外观,我已经更新了原始项目以反射(reflect)以下代码:

<View style={{flexDirection: 'row', justifyContent: 'flex-end'}}>
<Text>Floating right</Text>
</View>

我已经用上面的例子建立了一个完整的工作项目 here , 并粘贴下面项目的完整代码。希望这可以帮助!

https://rnplay.org/apps/KqE-cA

'use strict';

var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
} = React;

var SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<View style={{marginTop:70}}>
<TouchableHighlight style={{height:70, backgroundColor: 'blue'}}>
<View style={{flexDirection: 'column', }}>
<Text style={{textAlign:'center', color: 'white', fontSize:22, marginTop:10}}>MAIN TEXT</Text>
<Text style={{textAlign:'center', color: 'white', fontSize:14}}>Subtitle</Text>
</View>
</TouchableHighlight>
</View>

<View style={{marginTop:20}}>
<Text style={styles.mainStyle}>This text is red, <Text style={styles.blacktext}>this text is black, </Text><Text style={styles.boldgreen}>this text is bold and green!</Text></Text>
</View>

<View style={{marginTop:20}}>
<TouchableHighlight style={{height:70, backgroundColor: 'orange'}}>
<View style={{ flexDirection: 'row', justifyContent:'center'}}>
<Text style={{fontSize:18, marginTop: 22}}><Text style={{color: 'green'}}>Green</Text> - best Choice</Text>
</View>
</TouchableHighlight>
</View>

</View>
);
}
});

var styles = StyleSheet.create({
container: {
flex: 1,
},
mainStyle: {
color: 'red'
},
blacktext: {
color: 'black'
},
boldgreen: {
color: 'green',
fontWeight: 'bold'
}

});

AppRegistry.registerComponent('SampleApp', () => SampleApp);

关于flexbox - 文本组件的样式部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34045706/

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