Text作为创建UI时最基础的组件,用于在页面上展示文本。并且它也支持嵌套、样式,以及触摸处理。
基础用法如下:
render() {
return (
<View style={styles.container}>
<Text>
Hello World
</Text>
</View>
);
}
属性
adjustsFontSizeToFit bool
指定字体是否随着给定样式的限制而自动缩放。
allowFontScaling bool
控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。
iosminimumFontScale bool
当adjustsFontSizeToFit开启时,指定最小的缩放比(即不能低于这个值)。可设定的值为0.01 - 1.0
numberOfLines number
用来当文本过长的时候裁剪文本。包括折叠产生的换行在内,总的行数不会超过这个属性的限制。
onLayout function
当挂载或者布局变化以后调用,参数为如下的内容:
{nativeEvent: {layout: {x, y, width, height}}}
onLongPress function
当文本被长按以后调用此回调函数。
onPress function
当文本被点击以后调用此回调函数。
selectable function
决定用户是否可以长按选择文本,以便复制和粘贴。
stylestyle
fontFamily string
fontSize number
fontStyle enum('normal', 'italic')
fontWeightenum('normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')指定字体的粗细。大多数字体都支持'normal'和'bold'值。并非所有字体都支持所有的数字值。如果某个值不支持,则会自动选择最接近的值。
lineHeight number
textAlign enum('auto', 'left', 'right', 'center', 'justify')指定文本的对齐方式。其中'justify'值仅iOS支持,在Android上会变为left
textDecorationLine enum('none', 'underline', 'line-through', 'underline line-through')
textShadowColor color
textShadowOffset{width: number, height: number}
textShadowRadius number
androidincludeFontPadding boolAndroid在默认情况下会为文字额外保留一些padding,以便留出空间摆放上标或是下标的文字。对于某些字体来说,这些额外的padding可能会导致文字难以垂直居中。如果你把textAlignVertical
设置为center
之后,文字看起来依然不在正中间,那么可以尝试将本属性设置为false
.
androidtextAlignVertical enum('auto', 'top', 'bottom', 'center')
iosfontVariant[enum('small-caps', 'oldstyle-nums', 'lining-nums', 'tabular-nums', 'proportional-nums')]
iosletterSpacing number
iostextDecorationColor color
iostextDecorationStyle enum('solid', 'double', 'dotted', 'dashed')
ioswritingDirection enum('auto', 'ltr', 'rtl')