文本组件的样式部分

Ada*_*atz 1 flexbox react-native

我正在尝试对本机组件进行反应,其中同一组件中的不同单词具有不同的样式。

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

<Text style={styles.buttonText}>{speciesArry[i].title} {"\n"}       {speciesArry[i].subtitle}</Text>
Run Code Online (Sandbox Code Playgroud)

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

第二个例子

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

Nad*_*bit 5

对于第一个问题,一个带有标题和副标题的按钮,可以将一个视图包裹在一个 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>
Run Code Online (Sandbox Code Playgroud)

对于第二个问题,在单个文本组件中具有不同的样式,您需要将文本组件包装在文本组件中,就像在 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>
Run Code Online (Sandbox Code Playgroud)

您上面提供的按钮示例如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<View style={{flexDirection: 'row', justifyContent: 'flex-end'}}>
    <Text>Floating right</Text>
</View>
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)