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
- SwiftUI
- UIKit
textFormatters replaces the default list, which is one CometChatMentionsFormatter.
Include one to keep mentions: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.
Related
- Custom Text Formatter Guide — a formatter step by step.
- Customization Overview — every customization category.