Skip to main content
Text formatters process message text: they react to a tracking character typed in the composer, offer suggestions, and mark up the text in the conversation list and the composer. Use them for hashtags, custom mentions or any pattern. There is one protocol in CometChatCore, used by both toolkits. A formatter you write works in SwiftUI and UIKit unchanged.

CometChatTextFormatter API

A formatter is a @MainActor class that holds one CometChatTextFormatterState — created with the trackingCharacter that triggers it (@ for mentions, # for hashtags) — and implements search(_:) and onScrollToBottom(). Every other requirement has a default. Formatters work on Foundation AttributedString.

Key Methods

Suggestion System

CometChatTextFormatterState is @Observable: the composer redraws when you change it.

Marking up text

Mark a run with the Kit’s attributes and both toolkits draw it: RichTextFormatsAttribute takes a set of RichTextFormat — .bold, .italic, .underline, .strikethrough, .inlineCode, .link.

Example: Custom Hashtag Formatter

One implementation for both toolkits:
Formatters render live in the composer while typing through applyComposerSpans(_:), and the prepare…Span methods are applied to the reply and edit previews and the conversation list’s preview — register the formatter on each component that shows the text. The message bubbles do not run your formatter: they draw mentions and markdown from the message itself. To change how a text bubble draws, replace its bubble — see Message Template.

Registering Formatters

textFormatters replaces the default list, which is one CometChatMentionsFormatter. Include one to keep mentions:
To put the two on a screen, host the composer over the list as Conversation List + Message View shows.

Built-in Formatter: CometChatMentionsFormatter

CometChatMentionsFormatter handles @ mentions: user and group-member suggestions, <@uid:…> tokens on the wire shown as @Name, mention-all, and the mention colours. It is in CometChatCore and is each component’s default formatter.
See the Mentions Formatter Guide for every option.