Skip to main content
Text formatters let you process message text with tracking characters, suggestion lists, and spannable transformations. Use them to add hashtag detection, custom mentions, link previews, or any text pattern processing.

CometChatTextFormatter

The CometChatTextFormatter abstract class is the base for all formatters. Its constructor takes a char trackingCharacter that triggers the formatter when typed in the composer (e.g., @ for mentions, # for hashtags).

Key Override Methods

Pre-Send Hook

Override handlePreMessageSend(Context, BaseMessage) to modify a message before it’s sent. This is useful for attaching metadata, transforming text, or adding custom data to the message object.

Suggestion System

The formatter provides a built-in suggestion dropdown:

Example: Custom Hashtag Formatter

Registering Formatters

Register formatters on a component using setTextFormatters:

Built-in Formatter: CometChatMentionsFormatter

The UI Kit includes CometChatMentionsFormatter as a built-in formatter that handles @mention detection, user suggestion lists, and spannable highlighting. It serves as a reference implementation for building custom formatters. See the Mentions Formatter Guide for details.

Global Configuration via DataSource

Formatters can also be configured globally using the DataSource framework. Override getTextFormatters in a DataSourceDecorator to add formatters that apply across all components: