Skip to main content

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 ToolbarTrailingButtonsView wraps the current selection.
The marker is your own. It travels on the message exactly as you write it, so the same marker can be read by your web and mobile apps with a matching formatter on each.
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 CometChatMessageList and CometChatCompactMessageComposer
ToolbarTrailingButtonsView exists on CometChatCompactMessageComposer only. CometChatMessageComposer has no formatting toolbar, so there is nowhere to mount the button.

Step 1: The Formatter

Extend CometChatTextFormatter. 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 with textFormatters and mount the button with ToolbarTrailingButtonsView. File: ChatScreen.tsx
Now the user selects text, taps 🎨, and the input becomes 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.tsx
Media captions, the quoted reply inside a bubble, the reply and edit previews above the composer, and the message information screen all inherit the formatter from the component that renders them — you do not register those separately.
A formatter is only applied where you register it. If you add textFormatters to the composer but not the message list, the author sees the color but readers see raw {color=...} text. Register it on every surface that displays the message.

How It Round-Trips

The marker is plain text on the message, so it survives storage and delivery untouched; each display surface turns it into color independently through the formatter you registered.

Next Steps