FormattedText
Styles parts of a plain string by matching patterns. Use when the text comes from elsewhere and you cannot wrap the parts by hand.
compliant
- Overview
- Props
Usage​
FormattedText takes a plain text and a list of rules, then styles the parts that match each rule's pattern. Use it when the text to format isn't known at build time — search results, user-generated content, server-driven copy with markdown-like markers — and you can't hand-write Strong, Em, Highlight or A inline.
For static text with known emphasis, prefer composing Text with those inline components directly.
Bold​
Each rule matches a pattern and applies typography props to the match. Use renderText to strip the markers from the displayed text.
This text has bold words and more bold in it.
Links​
Add onPress to make a match interactive. Matched capture groups are passed to both renderText and onPress, so you can show one string and act on another (e.g. label vs. url).
Click this link to open the url.
Highlighting​
Set mark: true to wrap the match in a semantic <mark> tag — appropriate for search-result highlighting.
Search results: matching words are wrapped in a semantic mark tag.
Multiple rules​
Pass several rules to format different patterns at once. Rules are applied in order, and each rule runs over the segments produced by the previous one — so a later rule can match text inside an earlier match.
This has bold and highlighted text together.
Rules apply in order, so matches can nest: highlighted with bold inside it.
Rule reference​
Each entry in rules is a TextFormattingRule:
| Field | Type | Description |
|---|---|---|
pattern | RegExp | Matched against the text. The g flag is applied automatically. |
renderText | (match: string, groups: string[]) => string | Maps a match to its displayed text (e.g. strip ** markers). |
onPress | (match: string, groups: string[]) => void | Makes the match an interactive A. |
mark | boolean | Wraps the match in a semantic <mark> tag. |
weight | TextProps["weight"] | Font weight for the match. |
italic | TextProps["italic"] | Renders the match italic. |
color | TextProps["color"] | Color token for the match. |
fontFamily | TextProps["fontFamily"] | Font family for the match. |
sx | TextProps["sx"] | Extra styles, merged on top of the root sx. |
groups is the full regex match array, so groups[1], groups[2]… are the capture groups.
murray and murray/mobile.FormattedText props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| align | 9 possibilities"unset""left""right""end""start""auto""center""justify"undefined | |||
| aria-disabled | booleanundefined | |||
| aria-label | stringundefined | |||
| aria-level | numberundefined | Heading level when paired with `role="heading"`. | ||
| asChild | 5 possibilitiesboolean"web""except-style""except-style-web"undefined | |||
| className | stringundefined | |||
| color | 4 possibilitiesundefined`$${string}`Variable<any>OpaqueColorValue | Color prop accepts Tamagui color tokens, ColorTokens, or dynamic token strings. Examples: "$grey900", "$indigo500", "$white", or dynamically constructed tokens like `$${name}900` | ||
| dir | "auto"Directionundefined | |||
| ellipsizeMode | "head""middle""tail""clip"undefined | |||
| fontFamily | FontTokensundefined | |||
| htmlFor | stringundefined | |||
| id | stringundefined | |||
| italic | booleanundefined | |||
| lang | LocaleOrLanguageundefined | |||
| maxFontSizeMultiplier | numbernullundefined | |||
| numberOfLines | numberundefined | |||
| onLayout | ((e: LayoutChangeEvent) => void)undefined | |||
| onTextLayout | 2 possibilitiesundefined(e: NativeSyntheticEvent<TextLayoutEventData>) => void | Fires on mobile with the list of rendered lines. Passes through to the underlying RN Text. | ||
| role | Roleundefined | |||
| rules* | TextFormattingRule[] | |||
| size | 9 possibilitiesundefinednumberVariable<any>"$text100""$text200""$text300""$text400""$text500"Partial<Record<"$xs" | "$ltVs" | "$vs" | "$ltSm" | "$sm" | "$ltMd" | "$md" | "$ltLg" | "$lg" | "$ltXl" | "$xl" | "$ltXxl" | "$xxl", TextSize>> | Font size and line height token, see `TypographyBaseProps["size"]`. Also accepts one size per breakpoint, e.g. `{ $xs: "$text200", $lg: "$text300" }`: widths that no key matches keep the default or inherited size. | ||
| sx | 2 possibilitiesundefinedOmit<TextProps, "fontStyle" | "fontFamily" | "fontWeight"> | |||
| tabIndex | stringnumberundefined | |||
| tag | "p""span"undefined | |||
| transform | 6 possibilities"none""capitalize""uppercase""lowercase""unset"undefined | |||
| weight | TextWeightundefined |
Style API​
Use sx for one-off adjustments. It accepts these style keys:
Each can be overridden per breakpoint with: