Text
Renders text in a <span>, or the element that its role picks.
import { Text } from 'react-native'
export function Byline({ author }) {
return (
<Text numberOfLines={1}>
By <Text style={{ fontWeight: 'bold' }}>{author}</Text>
</Text>
)
}
A Text starts at 14 px, black, in the system font, and keeps its spaces and line breaks, as on native. A Text inside a Text takes the font, color, alignment, and decoration of its parent. As on native, a View between the two ends that: a Text inside a View inside a Text starts from the defaults again.
Props
| Prop | On the web |
|---|---|
style | Compiles to atomic CSS. See Styles. |
numberOfLines | 1 keeps the text on one line and ends it with an ellipsis. A larger number clamps the text to that many lines. |
ellipsizeMode | tail (the default) and clip. head and middle show the ellipsis at the end for now. |
allowFontScaling, maxFontSizeMultiplier | With the opt-in, as on native: see Font scaling. A nested Text inherits both. |
selectable | The text is selectable by default, as web users expect. On native the default is false. false turns selection off. |
selectionColor | The background color of selected text. |
onPress, onPressIn, onPressOut | As on Pressable: a pointer, Enter or Space, or a click. The event is a GestureResponderEvent. |
onLongPress | A press held for 500 ms. onPress then doesn't fire. |
pressRetentionOffset | How far a pointer can leave the text and still press it, as on Pressable. |
suppressHighlighting | Ignored, as on Android. Only iOS highlights a pressed Text. |
disabled | Sets aria-disabled. The Text isn't pressable, so a pressable parent gets the press. |
role, accessibility props | See Semantic HTML. |
onLayout, pointerEvents | As on View. |
onTextLayout | The lines of the text, after each change of the lines. See Lines of the text. |
nativeID, id, testID | id, id, and data-testid. |
href | Web only. Renders an <a>. |
target, rel, download | Web only. Pass to the anchor. |
hrefAttrs | Web only. The react-native-web form of target, rel, and download. See Props from react-native-web. |
dataSet | Web only. The react-native-web form of data-* props. See Props from react-native-web. |
inert | Web only. The element and its children take no focus and no clicks, and screen readers skip them. |
suppressHydrationWarning | Web only. React doesn't report a difference from the server HTML in the text and the attributes of this element. See Server rendering. |
lang | Web only. The language of the text, for screen readers and hyphenation. |
aria-level | Web only. The heading level of role="heading", 1 to 6. |
onClick and the other DOM handlers | Web only. Pass to the element. On a pressable Text, they run before the press handlers, and both run. |
onStartShouldSetResponder, onResponderGrant, and the other responder props | As on View. On a pressable Text, the press holds the pointer, and they do nothing. On native, the press also calls your onResponderGrant, onResponderMove, onResponderRelease, and onResponderTerminate. |
As on native, only onPress or onLongPress makes a Text pressable. A Text with only onPressIn or onPressOut doesn't respond. A pressable Text that isn't disabled shows the pointer cursor, and a cursor in style wins.
Font scaling
On native, text grows with the user's font-size setting. On the web, the same setting is the root font size of the browser. Opt in to follow it:
:root {
--rn-font-unit: 0.0625rem;
}
Then a numeric fontSize and lineHeight of a Text or a TextInput scale with the root font size: fontSize: 16 is 1rem. The default size, 14, scales too. Padding, margins, and other sizes don't scale, as on native. letterSpacing doesn't scale, as on iOS.
| Prop | With the opt-in |
|---|---|
allowFontScaling | true (the default) scales the text. false keeps the size in px. |
maxFontSizeMultiplier | Caps the scale: with 1.5, fontSize: 16 grows to 24 at most. A value below 1 has no cap, as on native. |
Without the opt-in, the text keeps its size, and browser zoom still scales the page. PixelRatio.getFontScale() gives the root font size divided by 16 in both cases.
font-size on the rootWith the opt-in, a font-size on <html> changes the size of all text. With html { font-size: 62.5% }, for example, fontSize: 16 renders at 10 px.
You can also set --rn-font-unit on one element, so that only the text inside it scales.
Lines of the text
onTextLayout gets { nativeEvent: { lines } }, as on native. It fires after the first layout, and again when the lines change: a new width, text, style, or a font that loads. Each line has these fields:
| Field | Value |
|---|---|
text | The characters of the line, with the space or the line break at its end. |
x, y | The position of the line, relative to the content box of the Text: inside the padding and the border. |
width | The width of the characters, without the space at the end of the line. |
height | The lineHeight of the style. With no lineHeight, the height of the font. |
ascender, descender | The distance from the baseline to the top and to the bottom of the line. Together they make height. |
capHeight, xHeight | The height of T and of x in the font of the start of the line. |
<Text onTextLayout={(event) => setLineCount(event.nativeEvent.lines.length)}>{text}</Text>
With numberOfLines, you get only the lines that show, as on iOS. A nested Text has no lines of its own: the outermost Text gets the lines, with the text of the nested ones in them, as on native.
As on native, a pressable Text with no role has the role link. It's in the tab order, and Enter or Space presses it. With an href, it renders an <a>. For an action, set role="button", and it renders a <button>.
Ref
The ref is the DOM element. It has the measure methods of View. The type is TextInstance.