Skip to main content

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​

PropOn the web
styleCompiles to atomic CSS. See Styles.
numberOfLines1 keeps the text on one line and ends it with an ellipsis. A larger number clamps the text to that many lines.
ellipsizeModetail (the default) and clip. head and middle show the ellipsis at the end for now.
allowFontScaling, maxFontSizeMultiplierWith the opt-in, as on native: see Font scaling. A nested Text inherits both.
selectableThe text is selectable by default, as web users expect. On native the default is false. false turns selection off.
selectionColorThe background color of selected text.
onPress, onPressIn, onPressOutAs on Pressable: a pointer, Enter or Space, or a click. The event is a GestureResponderEvent.
onLongPressA press held for 500 ms. onPress then doesn't fire.
pressRetentionOffsetHow far a pointer can leave the text and still press it, as on Pressable.
suppressHighlightingIgnored, as on Android. Only iOS highlights a pressed Text.
disabledSets aria-disabled. The Text isn't pressable, so a pressable parent gets the press.
role, accessibility propsSee Semantic HTML.
onLayout, pointerEventsAs on View.
onTextLayoutThe lines of the text, after each change of the lines. See Lines of the text.
nativeID, id, testIDid, id, and data-testid.
hrefWeb only. Renders an <a>.
target, rel, downloadWeb only. Pass to the anchor.
hrefAttrsWeb only. The react-native-web form of target, rel, and download. See Props from react-native-web.
dataSetWeb only. The react-native-web form of data-* props. See Props from react-native-web.
inertWeb only. The element and its children take no focus and no clicks, and screen readers skip them.
suppressHydrationWarningWeb only. React doesn't report a difference from the server HTML in the text and the attributes of this element. See Server rendering.
langWeb only. The language of the text, for screen readers and hyphenation.
aria-levelWeb only. The heading level of role="heading", 1 to 6.
onClick and the other DOM handlersWeb only. Pass to the element. On a pressable Text, they run before the press handlers, and both run.
onStartShouldSetResponder, onResponderGrant, and the other responder propsAs 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:

global.css
: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.

PropWith the opt-in
allowFontScalingtrue (the default) scales the text. false keeps the size in px.
maxFontSizeMultiplierCaps 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.

Set no font-size on the root

With 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:

FieldValue
textThe characters of the line, with the space or the line break at its end.
x, yThe position of the line, relative to the content box of the Text: inside the padding and the border.
widthThe width of the characters, without the space at the end of the line.
heightThe lineHeight of the style. With no lineHeight, the height of the font.
ascender, descenderThe distance from the baseline to the top and to the bottom of the line. Together they make height.
capHeight, xHeightThe 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.

A pressable text is a link

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.