[关闭]
@yzzer 2020-05-20T10:15:01.000000Z 字数 2122 阅读 621

React Native 文本组件 Text

在 React Native 中如果要显示一段文本,可以使用 React Native 内置的文本组件 ``。

文本组件 Text 只能用来显示文本,如果要显示网页,可以使用网页组件 WebView。

虽然文本组件可能将部分文本显示为电话号码或者网址等可以点击的样子,但毕竟有限。没有 WebView 来的强大。

React Native 文本组件 Text

引入组件

使用文本组件 Text 之前先要引入它

  1. import { Text } from 'react-native';

使用语法

  1. <Text
  2. color="#333333"
  3. lineHeight="12"
  4. fontSize="12" >简单教程</Text>

属性说明

属性 类型 是否必填 说明
selectable bool false 是否可选中,true 为真,false 为否
numberOfLines number false 用于在计算文本布局(包括换行)后使用省略号截断文本,使得总行数不超过此数字
ellipsizeMode string false 如果设置了 numberOfLines,那么该属性用于设置文本如何被截断
dataDetectorType string false 用于设置如何转换文本中的某些子文本
color color 否 用于设置文本的颜色
fontFamily string 否 用于设置文本的字体
fontSize number 否 用于设置文字的大小
fontStyle string 否 用于设置文字是否倾斜,normal 正常,italic 倾斜,默认为 normal
fontWeight string 否 文字的粗细,可以设置的值有: 'normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900'
lineHeight number 否 用于设置文本的行高
textAlign string 否 用于设置文本的对其方式,可选的值有 'auto', 'left', 'right', 'center', 'justify'。Android 下只有 left 即使设置了其它值扔就是 left
textDecorationLine string 否 用于设置文本的下划线类型,可选的值有 'none', 'underline', 'line-through', 'underline line-through'
textShadowColor color 否 用于设置文本的阴影色
textShadowOffset object 否 用于设置阴影的偏移量,格式为 {width: number,height: number}
textShadowRadius number 否 用于设置阴影的圆角度。
letterSpacing number 否 用于设置字与字之间的距离
textTransform string 否 用于设置文本转换格式,可选的值有 'none', 'uppercase', 'lowercase', 'capitalize'

组件嵌套

文本组件 `` 可以嵌套另一个组件,被嵌套的组件会继承父级的文本组件的样式和属性。

  1. <Text
  2. color="#333333"
  3. lineHeight="12"
  4. fontSize="12" >简单教程
  5. <Text
  6. fontSize="11"
  7. >简单编程</Text>
  8. </Text>

范例

下面的代码,我们演示了 React Native 文本组件的用法,也演示了文本组件的嵌套语法。

  1. import React, { Component } from 'react';
  2. import { View, Text, Image, StyleSheet } from 'react-native'
  3. const App = () => {
  4. return (
  5. <View style = {styles.container}>
  6. <Text style = {styles.text}>
  7. <Text style = {styles.capitalLetter}>
  8. B
  9. </Text>
  10. <Text>
  11. UCT
  12. </Text>
  13. <Text>
  14. BU<Text style = {styles.wordBold}>编程</Text> 怎样学习全英文课程
  15. </Text>
  16. <Text style = {styles.italicText}>阅读文档
  17. </Text>
  18. <Text style = {styles.textShadow}>
  19. 习惯全英文素材
  20. </Text>
  21. </Text>
  22. </View>
  23. )
  24. }
  25. export default App
  26. const styles = StyleSheet.create ({
  27. container: {
  28. alignItems: 'center',
  29. marginTop: 100,
  30. padding: 20
  31. },
  32. text: {
  33. color: '#41cdf4',
  34. },
  35. capitalLetter: {
  36. color: 'red',
  37. fontSize: 20
  38. },
  39. wordBold: {
  40. fontWeight: 'bold',
  41. color: 'black'
  42. },
  43. italicText: {
  44. color: '#37859b',
  45. fontStyle: 'italic'
  46. },
  47. textShadow: {
  48. textShadowColor: 'red',
  49. textShadowOffset: { width: 2, height: 2 },
  50. textShadowRadius : 5
  51. }
  52. })
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注