Skip to main content
CometChatTextFormatter is the protocol behind the Kit’s mentions. Conform to it to add your own trigger — # for hashtags, ! for shortcuts — with a suggestion list in the composer, and to rewrite how message text reads in the lists that run formatters. It is one protocol for both toolkits, in CometChatCore. The stored state lives in a CometChatTextFormatterState your formatter owns.

How a formatter works

The message bubbles do not run your formatter. They draw mentions and markdown from the message itself, so a custom prepareLeftMessageBubbleSpan / prepareRightMessageBubbleSpan does not change a bubble. To change how a text bubble draws, replace its bubble — see Message Template.

Steps

1. Conform to CometChatTextFormatter

Hold one CometChatTextFormatterState, created with your tracking character:
HashtagFormatter.swift

2. Fill the suggestion list from search(_:)

search(_:) runs as the user types after the tracking character. Answer with setSuggestionItemList(_:); show a spinner while you fetch with setShowLoadingIndicator(_:); put a line above the list with setInfoText(_:) and setInfoVisibility(_:). A SuggestionItem:

3. Load more in onScrollToBottom()

Called when the list reaches its end. Fetch the next page and append to suggestionItemList with setSuggestionItemList(_:). Leave it empty if you load everything at once.

4. Rewrite text in the lists (optional)

The prepare…Span methods take the message and the text so far and return the text to show. Every one defaults to returning the text unchanged. prepareConversationSpan changes the last message line in Conversations:
TagStripper.swift
The other hooks: preparePreviewSpan (the composer’s reply and edit previews; by default prepareComposerSpan), getOriginalText (your display markup back to its wire text) and handlePreMessageSend (change the message — its metadata, say — just before it is sent).

5. Give it to a component

textFormatters replaces the composer’s list. nil, the default, means one mentions formatter — include a CometChatMentionsFormatter to keep mentions. Keep the formatters in @State so they are not rebuilt on every render:
TaggedComposer.swift
CometChatConversations(textFormatters:), CometChatMessageList(textFormatters:), CometChatThreadHeader(textFormatters:), CometChatPinnedMessages(textFormatters:) and CometChatSavedMessages(textFormatters:) take a list too. Give each component its own instances — formatters hold state.
The SwiftUI composer’s disableMentions: true removes every formatter, yours included. To keep yours without mentions, pass a list with no mentions formatter.

Methods Reference

Next Steps

Mentions Formatter

The built-in formatter, configured

ShortCut Formatter

A formatter over the message-shortcuts extension

Message Composer

Where suggestions appear

All Guides

Browse the other guides