一、简言

初学RN,一切皆新。Text组件主要用于显示文本,Text组件的重要性不言而喻,无论是Web开发还是客户端开发,都离不开它。它具有响应特性,也即表现为当它被触摸时是否显示为高亮状态。在Web开发中,字体样式的继承十分重要,在body上设置字体可以作用到全部的文本标签,而RN中字体样式只有在Text组件上才起作用。它支持多层嵌套,由此它存在样式继承,内部的Text组件可以继承外部Text组件的样式,也即父组件定义了相关样式,如果子组件没有重写样式的话,那么该子组件也会继承父组件定义的样式。Text组件的布局方式和View组件的布局方式不同,View组件采用FlexBox伸缩盒子布局,而Text组件则采用的是文本布局,一个接一个Text组件横向排列,如果文本到达末尾,可以换行显示。总体来说,它的主要作用概括是显示基本的文本信息,除了基本的显示布局之外,也可以进行嵌套布局,设置样式,以及做事件处理。

二、特性

onPress:该属性的值是一个函数,表示按下事件或者叫手指触摸事件。当手指按下时被触发。

numberOfLines:该属性的值是一个数字,表示显示行数,如果超过该数值,则使用省略号{...}显示。

allowFontScalling:该属性的值是一个函数,控制字体是否根据iOS的设置进行自动缩放。

onLayout:该属性的值是一个函数,用于获取该元素布局的位置和大小。例如{"target":7,"layout":{"x":10,"y":10,"width":100,"height":100}}。一般函数的事件形式为:

//打印事件参数
function(e){ console.log(e.nativeEvent) };

三、样式

除了继承了View组件的所有Style外,本身还具有如下样式属性:

color:字体颜色 
fontFamily:字体名称
fontStlye:字体风格(normal,italic)
fontWeight:字体粗细('normal','bold','100','200')
fontSize:字体大小
textShadowOffset:设置阴影效果{width:number,height:number}
textShaowRadius:阴影效果圆角
textShadowColr:阴影效果的颜色
letterSpacing:字符间距
lineHeight:行高
textAlign:文本对齐方式('auto','left','right','')
textDecorationLine:横线位置('none','underline','line-through','underline line-through')
textDecorationStyle:线的风格('solid','')
textDecorationColor:线的颜色
writingDirection:文本方向('auto','ltr','rtl')

四、示例

注意:下面代码中引入了PixelRatio API,PixelRatio的get方法用于获取高清设备的像素比。

NavHead.js:

//采用React.createClass创建组件
const React = require('react');
const ReactNative = require('react-native')
const {
StyleSheet,
View,
Text,
PixelRatio
} = ReactNative; const NavHead = React.createClass({ //打印事件参数
print(e){
console.log(e.nativeEvent)
}, render(){
return (
<View style={styles.parent}>
<View style={styles.flex}>
<Text style={styles.title}>
<Text style={styles.title1} onPress={this.print}>網易</Text>
<Text style={styles.title2}>新闻</Text>
<Text style={styles.pk}> pk </Text>
<Text style={styles.title1}>紟日</Text>
<Text style={styles.title2}>头条</Text>
</Text>
</View>
</View>
)
}
}); const styles = StyleSheet.create({
parent:{
height: 75,
backgroundColor: '#EEE'
},
flex: {
marginTop: 25,
height: 50,
borderBottomWidth: 2/PixelRatio.get(),
borderBottomColor: '#EF2D36',
alignItems: 'center'
},
title: {
fontSize: 25,
fontWeight: 'bold',
textAlign: 'center'
},
pk: {
color: 'green'
},
title1 :{
color: '#CD1D1C'
},
title2: {
color: '#FFFFFF',
backgroundColor: '#CD1D1C'
}
}); module.exports = NavHead;

index.ios.js:

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/ import React, { Component } from 'react';
import NavHead from './src/NavHead'
import List from './src/List' import {
AppRegistry,
StyleSheet,
View
} from 'react-native'; export default class ReactNativeDemo extends Component { render() {
return (
<View style={styles.container}>
<NavHead/>
<List/>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white'
}
}); AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);

五、打印

-- ::46.298 [info][tid:com.facebook.react.JavaScript] { target: ,
pageY: 42.5,
locationX: ,
force: ,
locationY: 17.5,
identifier: ,
pageX: 83.5,
timestamp: 143715461.59643,
changedTouches: [ [Circular] ],
touches: [] }

最新文章

  1. iOS 自定义UIButton(图片和文字混合)
  2. 使用 Velocity 模板引擎快速生成代码(zhuan)
  3. 安装Spark集群(在CentOS上)
  4. PHP MySQL Insert Into 之 Insert
  5. USB调试不能弹出授权窗口 unauthorized 的解决办法
  6. qt程序启动画面
  7. Linux 内核配置机制(make menuconfig、Kconfig、makefile)讲解
  8. 快速排序-python
  9. 数据库和Content Provider
  10. HBase之Table.put客户端流程(续)
  11. 2017-9-14-Linux移植:加快Linux主机的启动速度
  12. 安装phpssdbadmin
  13. static关键字(修饰函数、局部变量、全局变量)
  14. 处理【由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面】
  15. 即时通信系统Openfire分析之二:主干程序分析
  16. AngularJS概述-3D
  17. es修改索引副本个数
  18. MVC4+EF5 edmx代码分析
  19. ActiveMQ之ActiveMQ-CPP安装及测试
  20. 在vue-cli中使用路由

热门文章

  1. IDEA新建servlet时出现的错误
  2. Linux基础命令---ntpstat显示时间服务器同步
  3. [Go] golang中的包管理
  4. [考试反思]1113csp-s模拟测试113:一念
  5. 云数据库MongoDB版清理oplog日志和compact命令详解
  6. IT兄弟连 HTML5教程 CSS3揭秘 CSS常见的样式属性和值5
  7. Linux中,Tomcat 怎么承载高并发(深入Tcp参数 backlog)
  8. window.innerHeight和document.documentElement.clientHeight区别
  9. ZooKeeper(五):事务处理之更新数据逻辑解析
  10. C# 调用POST请求