Text

A react component for displaying text which supports nesting, styling, and touch handling. In the following example, the nested title and body text will inherit the fontFamily from styles.baseText, but the title provides its own additional styles. The title and body will stack on top of each other on account of the literal newlines:

renderText: function() { return ( <Text style={styles.baseText}> <Text style={styles.titleText} onPress={this.onPressTitle}> {this.state.titleText + '\n\n'} </Text> <Text numberOfLines={5}> {this.state.bodyText} </Text> </Text> ); }, ... var styles = StyleSheet.create({ baseText: { fontFamily: 'Cochin', }, titleText: { fontSize: 20, fontWeight: 'bold', }, };

Edit on GitHubProps #

numberOfLines number #

Used to truncate the text with an elipsis after computing the text layout, including line wrapping, such that the total number of lines does not exceed this number.

onPress function #

This function is called on press. Text intrinsically supports press handling with a default highlight state (which can be disabled with suppressHighlighting).

style style #

color string
containerBackgroundColor string
fontFamily string
fontSize number
fontStyle enum('normal', 'italic')
fontWeight enum("normal", 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')
lineHeight number
textAlign enum("auto", 'left', 'right', 'center')
writingDirection enum("auto", 'ltr', 'rtl')

suppressHighlighting bool #

When true, no visual change is made when text is pressed down. By default, a gray oval highlights the text on press down.

testID string #

Used to locate this view in end-to-end tests.

Edit on GitHubDescription #

Nested Text #

In iOS, the way to display formatted text is by using NSAttributedString: you give the text that you want to display and annotate ranges with some specific formatting. In practice, this is very tedious. For React Native, we decided to use web paradigm for this where you can nest text to achieve the same effect.

<Text style={{fontWeight: 'bold'}}> I am bold <Text style={{color: 'red'}}> and red </Text> </Text>

Behind the scenes, this is going to be converted to a flat NSAttributedString that contains the following information

"I am bold and red" 0-9: bold 9-17: bold, red

Containers #

The <Text> element is special relative to layout: everything inside is no longer using the flexbox layout but using text layout. This means that elements inside of a <Text> are no longer rectangles, but wrap when they see the end of the line.

<Text> <Text>First part and </Text> <Text>second part</Text> </Text> // Text container: all the text flows as if it was one // |First part | // |and second | // |part | <View> <Text>First part and </Text> <Text>second part</Text> </View> // View container: each text is its own block // |First part | // |and | // |second part|

Limited Style Inheritance #

On the web, the usual way to set a font family and size for the entire document is to write:

/* CSS, *not* React Native */ html { font-family: 'lucida grande', tahoma, verdana, arial, sans-serif; font-size: 11px; color: #141823; }

When the browser is trying to render a text node, it's going to go all the way up to the root element of the tree and find an element with a font-size attribute. An unexpected property of this system is that any node can have font-size attribute, including a <div>. This was designed for convenience, even though not really semantically correct.

In React Native, we are more strict about it: you must wrap all the text nodes inside of a <Text> component; you cannot have a text node directly under a <View>.

// BAD: will fatal, can't have a text node as child of a <View> <View> Some text </View> // GOOD <View> <Text> Some text </Text> </View>

You also lose the ability to set up a default font for an entire subtree. The recommended way to use consistent fonts and sizes across your application is to create a component MyAppText that includes them and use this component across your app. You can also use this component to make more specific components like MyAppHeaderText for other kinds of text.

<View> <MyAppText>Text styled with the default font for the entire application</MyAppText> <MyAppHeaderText>Text styled as a header</MyAppHeaderText> </View>

React Native still has the concept of style inheritance, but limited to text subtrees. In this case, the second part will be both bold and red.

<Text style={{fontWeight: 'bold'}}> I am bold <Text style={{color: 'red'}}> and red </Text> </Text>

We believe that this more constrained way to style text will yield better apps:

  • (Developer) React components are designed with strong isolation in mind: You should be able to drop a component anywhere in your application, trusting that as long as the props are the same, it will look and behave the same way. Text properties that could inherit from outside of the props would break this isolation.

  • (Implementor) The implementation of React Native is also simplified. We do not need to have a fontFamily field on every single element, and we do not need to potentially traverse the tree up to the root every time we display a text node. The style inheritance is only encoded inside of the native Text component and doesn't leak to other components or the system itself.

Edit on GitHubExamples #

'use strict'; var React = require('react-native'); var { StyleSheet, Text, View, } = React; var Entity = React.createClass({ render: function() { return ( <Text style={styles.entity}> {this.props.children} </Text> ); } }); var AttributeToggler = React.createClass({ getInitialState: function() { return {fontWeight: '500', fontSize: 15}; }, increaseSize: function() { this.setState({ fontSize: this.state.fontSize + 1 }); }, render: function() { var curStyle = {fontSize: this.state.fontSize}; return ( <Text> <Text style={curStyle}> Tap the controls below to change attributes. </Text> <Text> See how it will even work on{' '} <Text style={curStyle}> this nested text </Text> <Text onPress={this.increaseSize}> {'>> Increase Size <<'} </Text> </Text> </Text> ); } }); exports.title = '<Text>'; exports.description = 'Base component for rendering styled text.'; exports.displayName = 'TextExample'; exports.examples = [ { title: 'Wrap', render: function() { return ( <Text> The text should wrap if it goes on multiple lines. See, this is going to the next line. </Text> ); }, }, { title: 'Padding', render: function() { return ( <Text style={{padding: 10}}> This text is indented by 10px padding on all sides. </Text> ); }, }, { title: 'Font Family', render: function() { return ( <View> <Text style={{fontFamily: 'Cochin'}}> Cochin </Text> <Text style={{fontFamily: 'Cochin', fontWeight: 'bold'}}> Cochin bold </Text> <Text style={{fontFamily: 'Helvetica'}}> Helvetica </Text> <Text style={{fontFamily: 'Helvetica', fontWeight: 'bold'}}> Helvetica bold </Text> <Text style={{fontFamily: 'Verdana'}}> Verdana </Text> <Text style={{fontFamily: 'Verdana', fontWeight: 'bold'}}> Verdana bold </Text> </View> ); }, }, { title: 'Font Size', render: function() { return ( <View> <Text style={{fontSize: 23}}> Size 23 </Text> <Text style={{fontSize: 8}}> Size 8 </Text> </View> ); }, }, { title: 'Color', render: function() { return ( <View> <Text style={{color: 'red'}}> Red color </Text> <Text style={{color: 'blue'}}> Blue color </Text> </View> ); }, }, { title: 'Font Weight', render: function() { return ( <View> <Text style={{fontWeight: '100'}}> Move fast and be ultralight </Text> <Text style={{fontWeight: '200'}}> Move fast and be light </Text> <Text style={{fontWeight: 'normal'}}> Move fast and be normal </Text> <Text style={{fontWeight: 'bold'}}> Move fast and be bold </Text> <Text style={{fontWeight: '900'}}> Move fast and be ultrabold </Text> </View> ); }, }, { title: 'Font Style', render: function() { return ( <View> <Text style={{fontStyle: 'normal'}}> Normal text </Text> <Text style={{fontStyle: 'italic'}}> Italic text </Text> </View> ); }, }, { title: 'Nested', description: 'Nested text components will inherit the styles of their ' + 'parents (only backgroundColor is inherited from non-Text parents). ' + '<Text> only supports other <Text> and raw text (strings) as children.', render: function() { return ( <View> <Text> (Normal text, <Text style={{fontWeight: 'bold'}}> (and bold <Text style={{fontSize: 11, color: '#527fe4'}}> (and tiny inherited bold blue) </Text> ) </Text> ) </Text> <Text style={{fontSize: 12}}> <Entity>Entity Name</Entity> </Text> </View> ); }, }, { title: 'Text Align', render: function() { return ( <View> <Text style={{textAlign: 'left'}}> left left left left left left left left left left left left left left left </Text> <Text style={{textAlign: 'center'}}> center center center center center center center center center center center </Text> <Text style={{textAlign: 'right'}}> right right right right right right right right right right right right right </Text> </View> ); }, }, { title: 'Spaces', render: function() { return ( <Text> A {'generated'} {' '} {'string'} and some &nbsp;&nbsp;&nbsp; spaces </Text> ); }, }, { title: 'Line Height', render: function() { return ( <Text> <Text style={{lineHeight: 35}}> A lot of space between the lines of this long passage that should wrap once. </Text> </Text> ); }, }, { title: 'Empty Text', description: 'It\'s ok to have Text with zero or null children.', render: function() { return ( <Text /> ); }, }, { title: 'Toggling Attributes', render: function(): ReactElement { return <AttributeToggler />; }, }, { title: 'backgroundColor attribute', description: 'backgroundColor is inherited from all types of views.', render: function() { return ( <View style={{backgroundColor: 'yellow'}}> <Text> Yellow background inherited from View parent, <Text style={{backgroundColor: '#ffaaaa'}}> {' '}red background, <Text style={{backgroundColor: '#aaaaff'}}> {' '}blue background, <Text> {' '}inherited blue background, <Text style={{backgroundColor: '#aaffaa'}}> {' '}nested green background. </Text> </Text> </Text> </Text> </Text> </View> ); }, }, { title: 'containerBackgroundColor attribute', render: function() { return ( <View> <View style={{flexDirection: 'row', height: 85}}> <View style={{backgroundColor: '#ffaaaa', width: 150}} /> <View style={{backgroundColor: '#aaaaff', width: 150}} /> </View> <Text style={[styles.backgroundColorText, {top: -80}]}> Default containerBackgroundColor (inherited) + backgroundColor wash </Text> <Text style={[ styles.backgroundColorText, {top: -70, containerBackgroundColor: 'transparent'}]}> {"containerBackgroundColor: 'transparent' + backgroundColor wash"} </Text> </View> ); }, }, { title: 'numberOfLines attribute', render: function() { return ( <View> <Text numberOfLines={1}> Maximum of one line no matter now much I write here. If I keep writing it{"'"}ll just truncate after one line </Text> <Text numberOfLines={2} style={{marginTop: 20}}> Maximum of two lines no matter now much I write here. If I keep writing it{"'"}ll just truncate after two lines </Text> <Text style={{marginTop: 20}}> No maximum lines specified no matter now much I write here. If I keep writing it{"'"}ll just keep going and going </Text> </View> ); }, }]; var styles = StyleSheet.create({ backgroundColorText: { left: 5, backgroundColor: 'rgba(100, 100, 100, 0.3)' }, entity: { fontWeight: '500', color: '#527fe4', }, });