React Native

React Native学习(四)-- 常用组件之Text

2017-05-10  本文已影响13人  寒桥

一、什么是Text组件?

一个用于显示文本的React组件,和Android中的TextView组件或者OC中的Label组件相类似,专门用来显示基本的文本信息;除了基本的显示布局之外,可以进行嵌套显示,设置样式,以及可以做事件(例如:点击)处理;

二、Text组件常用的属性方法

   (1) color string  ** 字体颜色
   (2)containerBackgroundColor string
   (3)fontFamily string  ** 字体名称
   (4)fontSize number  ** 字体大小
   (5)fontStyle enum('normal', 'italic')  ** 字体风格(normal,italic) 斜体只适用于英文
   (6)fontWeight enum("normal", 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900') ** 字体粗细权重("normal", 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')
   (7) lineHeight number  ** 行高
   (8)textAlign enum("auto", 'left', 'right', 'center')  ** 文本对其方式("auto", 'left', 'right', 'center', 'justify')
   (9)writingDirection enum("auto", 'ltr', 'rtl')  **  文本方向("auto", 'ltr', 'rtl')
   (10)numberOfLines number  ** 进行设置Text显示文本的行数,如果显示的内容超过了行数,默认其他多余的信息就不会显示了 (RN中不可用)
   (11)onPress  fcuntion  ** 该方法当文本发生点击的时候调用该方法
   (12)textShadowOffset   ** 设置阴影效果{width: number, height: number}
   (13)textShadowRadius  ** 阴影效果圆角
   (14)textShadowColor  ** 阴影效果的颜色
   (15)letterSpacing  ** 字符间距
   (16)textDecorationLine  ** 横线位置 ("none", 'underline', 'line-through', 'underline line-through')
   (17)textDecorationStyle ** 线的风格("solid", 'double', 'dotted', 'dashed')
   (18)textDecorationColor ** 线的颜色

三、Text组件中常用属性的应用

export default class AHelloProject extends Component {
  render() {
    return (
      <View style={{marginTop: 50}}>
        <Text style={styles.textStyle}>
          这是Text组件常用属性的应用示例,{'\n'}
          <Text style={{backgroundColor: 'green',color: 'purple'}}>
            我是继承的Text
          </Text>
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  textStyle: {
    color: 'red',
    backgroundColor: 'blue',
    fontFamily: 'Times',
    fontSize: 25,
    fontStyle: 'italic',
    fontWeight: 'bold',
    lineHeight: 40,
    textAlign: 'center',
    writingDirection: 'rtl',
    letterSpacing: 5,
    textDecorationLine: 'underline line-through',
    textDecorationStyle: 'solid',
    textDecorationColor: 'yellow',
    textShadowOffset: {width: 10, height: 10},
    textShadowRadius: 5,
    textShadowColor: 'black'
  },

});
效果图.png

四、Text组件中样式的继承

在React-native中是没有样式继承这种说法的, 但是对于Text元素里边的Text元素,其实是能够继承的,那么是单层继承还是多层继承?

由上边的例子可以看出:<strong>文字控制类的属性也是多重继承的,和css中是一样的</strong>

上一篇 下一篇

猜你喜欢

热点阅读