Goal
By the end of this guide you will have a color formatter: a button in the composer’s formatting toolbar that wraps the selected text in a color marker, and a formatter that renders that marker as colored text everywhere the message appears — the message list, the conversation subtitle, search results, pinned and saved messages. A text formatter has two jobs, and this guide covers both:- Rendering — turn a marker in the raw message text into styled text wherever the message is displayed (
formatRawText()). - Authoring — give users a way to produce that marker. Here, a button in the composer’s
ToolbarTrailingButtonsViewwraps the current selection.
If you only need colored text inside a React Native app, the composer’s rich-text toolbar already colors a selection with no formatter at all — see Text Color. Use this guide when the marker has to be yours.
Prerequisites
- Completed the Integration Guide
- A chat screen using
CometChatMessageListandCometChatCompactMessageComposer
Step 1: The Formatter
ExtendCometChatTextFormatter. The method that matters for rendering is formatRawText(): it receives the raw message text, before any of the UI Kit’s own parsing, and returns a string. Our marker is {color=VALUE}...{/color}, and we turn it into the UI Kit’s color markup.
File: src/formatters/ColorFormatter.ts
formatRawText() runs before the built-in Markdown formatter. That ordering is what makes the formatter reliable: by the time Markdown has run, the text is no longer a string, so a formatter that only overrides getFormattedText() never sees your marker in a message that also contains _, **, - , [ or a backtick.Step 2: The Toolbar Button
ToolbarTrailingButtonsView renders your views at the end of the formatting toolbar. It is a render function and, besides user, group and composerId, it receives a composer handle — the same controls the built-in Bold and Italic buttons use.
composer.replaceSelection(text) swaps the current selection for your text, leaving the caret after it. With nothing selected it inserts at the caret.
File: src/components/ColorButton.tsx
Step 3: Wire It Into the Composer
Register the formatter withtextFormatters and mount the button with ToolbarTrailingButtonsView.
File: ChatScreen.tsx
Hello {color=#e5484d}world{/color}. On send, that raw text is stored on the message.
Step 4: Render It Everywhere the Message Appears
The marker only becomes color when a surface runs the formatter. Register the same instance on every surface where the message can show up. File: ChatScreen.tsxHow It Round-Trips
Next Steps
- Text Formatter Base Class — the full
CometChatTextFormatterAPI - Text Color — coloring text with the built-in rich-text toolbar, no formatter required
- Message Composer — the toolbar slot in detail