diff --git a/docs/text.md b/docs/text.md index c956884065f..8ab6ccd89df 100644 --- a/docs/text.md +++ b/docs/text.md @@ -9,16 +9,69 @@ A React component for displaying text. 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: -```SnackPlayer name=Text -import React, { Component } from 'react'; -import { Text, StyleSheet } from 'react-native'; +
+ +
+ + + +```SnackPlayer name=Text%20Functional%20Component%20Example +import React, { useState } from "react"; +import { Text, StyleSheet } from "react-native"; + +const onPressTitle = () => { + console.log("title pressed"); +}; + +const TextInANest = () => { + const titleText = useState("Bird's Nest"); + const bodyText = useState("This is not really a bird nest."); + + return ( + + + {titleText} + {"\n"} + {"\n"} + + {bodyText} + + ); +}; + +const styles = StyleSheet.create({ + baseText: { + fontFamily: "Cochin" + }, + titleText: { + fontSize: 20, + fontWeight: "bold" + } +}); + +export default TextInANest; + +``` + + + +```SnackPlayer name=Text%20Class%20Component%20Example +import React, { Component } from "react"; +import { Text, StyleSheet } from "react-native"; export default class TextInANest extends Component { constructor(props) { super(props); this.state = { titleText: "Bird's Nest", - bodyText: 'This is not really a bird nest.' + bodyText: "This is not really a bird nest." }; } @@ -26,11 +79,11 @@ export default class TextInANest extends Component { return ( - {this.state.titleText}{'\n'}{'\n'} - - - {this.state.bodyText} + {this.state.titleText} + {"\n"} + {"\n"} + {this.state.bodyText} ); } @@ -38,12 +91,12 @@ export default class TextInANest extends Component { const styles = StyleSheet.create({ baseText: { - fontFamily: 'Cochin', + fontFamily: "Cochin" }, titleText: { fontSize: 20, - fontWeight: 'bold', - }, + fontWeight: "bold" + } }); ``` @@ -51,24 +104,33 @@ const styles = StyleSheet.create({ Both Android and iOS allow you to display formatted text by annotating ranges of a string with specific formatting like bold or colored text (`NSAttributedString` on iOS, `SpannableString` on Android). 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. -```SnackPlayer name=Nested -import React, { Component } from 'react'; -import { Text } from 'react-native'; +```SnackPlayer name=Nested%20Text%20Example +import React from 'react'; +import { Text, StyleSheet } from 'react-native'; -export default class BoldAndBeautiful extends Component { - render() { - return ( - - I am bold - - and red - - - ); +const BoldAndBeautiful = () => { + return ( + + I am bold + and red + + ); +}; + +const styles = StyleSheet.create({ + baseText: { + fontWeight: 'bold' + }, + innerText: { + color: 'red' } -} +}); + +export default BoldAndBeautiful; ``` + + Behind the scenes, React Native converts this to a flat `NSAttributedString` or `SpannableString` that contains the following information: ```jsx