Overview
CometChatCompactMessageComposer is a Component that provides a compact, single-line message input designed for chat applications. It offers a streamlined interface with optional rich text formatting capabilities, supporting bold, italic, strikethrough, code, links, lists, and blockquotes.
Features such as Rich Text Formatting, Attachments, Message Editing, Mentions, Stickers, and Voice Recording are supported.
Usage
Integration
The following code snippet illustrates how you can directly incorporate the CompactMessageComposer component into your view.- Swift
To ensure that the
CometChatCompactMessageComposer is properly configured, passing the controller is mandatory.Actions
Actions dictate how a component functions. They are divided into two types: Predefined and User-defined. You can override either type, allowing you to tailor the behavior of the component to fit your specific needs.setOnSendButtonClick
Theset(onSendButtonClick:) event gets activated when the send message button is clicked. It has a predefined function of sending messages entered in the composer. However, you can override this action with the following code snippet.
- Swift
setOnError
This action doesn’t change the behavior of the component but rather listens for any errors that occur in the CompactMessageComposer component.- Swift
setOnTextChangedListener
Function triggered whenever the message input’s text value changes, enabling dynamic text handling.- Swift
setAttachmentOptions
This action is triggered when the attachment button is clicked. You can override the default attachment sheet behavior with a custom implementation.- Swift
Filters
CompactMessageComposer component does not have any available filters.Events
Events are emitted by aComponent. By using event you can extend existing functionality. Being global events, they can be applied in Multiple Locations and are capable of being Added or Removed.
The CompactMessageComposer Component does not emit any events of its own.
Customization
To fit your app’s design requirements, you can customize the appearance of the CompactMessageComposer component. We provide exposed methods that allow you to modify the experience and behavior according to your specific needs.Style
Using Style you can customize the look and feel of the component in your app. These parameters typically control elements such as the color, size, shape, and fonts used within the component. Global level styling- Swift
- Swift
CompactMessageComposerStyle:
Functionality
These are a set of small functional customizations that allow you to fine-tune the overall experience of the component. With these, you can change text, set custom icons, and toggle the visibility of UI elements. Below is a list of customizations along with corresponding code snippets:Multiple Attachments
The compact composer supports sending several attachments in one go. Tapping a media or file option opens a multi-select picker; the picks are staged in an attachment tray above the input box, upload immediately, and are sent together when the user taps send. How it works:- Multi-select picking — the photo/video picker and the document picker both allow selecting multiple items, capped to the app’s per-message limit. Camera captures and audio files stage into the same tray.
- Attachment tray — each staged file shows as a tile (thumbnail for media, chip for files/audio) with a live upload progress ring, a ✕ to cancel or remove, and tap-to-retry on network failure.
- Upload before send — files upload as soon as they are staged; the send button stays disabled until every upload finishes. Text typed in the composer becomes the caption of the batch, with rich text formatting preserved.
- One message per attachment type — a mixed pick is split into separate messages in a fixed order (images → videos → audios → files) that render as grouped bubbles in the message list.
- Clipboard paste — images, videos, documents and audio files copied to the clipboard can be pasted straight into the composer and stage into the tray.
- Voice notes stay standalone — a mic recording always sends immediately as its own message and is never staged. The mic button hides while attachments are staged and returns when the tray empties.
- Attachments cannot be added while editing a message; the attachment button is disabled for the duration of the edit. Editing a media message edits its caption only.
Limits and Validation
Limits are governed by the app’s server settings (CometChat dashboard) — the composer reads them at runtime and they cannot be overridden client-side:
Tapping an error tile shows a snack bar with the reason (“File exceeds N MB.”, “This file type isn’t allowed.”). The snack bar’s appearance is themable through
CometChatErrorStateStyle:
Disabling Multiple Attachments
SetenableMultipleAttachments to false to restore the legacy behavior — single-item pickers that send immediately with no tray:
Rich Text Formatting
The CompactMessageComposer includes a built-in rich text editor.enableRichTextFormatting and showRichTextFormattingOptions are true by default, so rich text formatting and the formatting toolbar are enabled out of the box.
enableRichTextFormatting— Master switch that activates formatting. When set tofalse, even manually typed markdown syntax like**text**will be sent as plain text.showRichTextFormattingOptions— Controls the visibility of the formatting toolbar below the input field.
Supported Format Types
Configuration
- Swift
RichTextToolbarStyle
Global level styling- Swift
RichTextToolbarStyle:
Trailing toolbar actions
set(richTextToolbarActions:) appends your own buttons at the trailing end of the built-in toolbar — after the format buttons, separated by a divider — without replacing the toolbar. It takes the same closure shape as set(attachmentOptions:), receiving the current User? and Group?, and returns a list of CometChatRichTextToolbarAction items.
Each action’s
onClick receives a CometChatComposerInput — a live handle for reading and mutating the draft, so the composer stays the owner of the field:
- Swift
The trailing section lives inside the rich-text toolbar — it is not rendered when the toolbar is hidden (
showRichTextFormattingOptions = false) or rich text is disabled (enableRichTextFormatting = false).The CometChatComposerInput is valid only for the duration of the onClick call; don’t retain it. The action itself is retained by the toolbar, so capture self weakly.The composer serializes to markdown on send. The built-in formats survive, and so does text colour — see Colour that survives send. Any other attribute you apply is composer-local: markdown has no representation for it, so it is dropped from the sent message. Use those for affordances that are meant to be transient, such as marking a range while the action’s own picker or sheet is open.onClick returns rather than per mutation, so a multi-step edit resyncs once. Call commit() to resync earlier.
Leaving Kit-owned runs alone
An action that restyles a range will happily run over a mention or a link, which the Kit repaints on its own schedule — so your attribute is reverted on the next repaint anyway, and removing one of the Kit’s attributes deletes it rather than restoring the Kit’s value. The mutating methods therefore takeprotectingKitRuns, which defaults to true. With it on, the following are carved out of the target range and left untouched:
- mentions
- links, including a URL still being typed
- inline code and code blocks, and any monospaced run
- fully transparent runs, so an action cannot repaint hidden text into view
protectingKitRuns: false to opt out and write over the whole range. To make a narrower decision yourself, read mentionRanges — the ranges are read at call time, so re-read them after a mutation rather than caching, as they shift with the text. Note that mentionRanges covers mentions only; the other carve-outs above have no public accessor.
- Swift
Colour that survives send
Text colour is the one value-carrying style with a wire representation: the composer serializes it as<color=#rrggbb>…</color>, and the message bubble parses it back. Colour therefore reaches the recipient, unlike other attributes you might apply.
Setting .foregroundColor on its own is not enough. That key is written by six different things — mentions, links, inline code, code blocks, ordinary text and the user — so it records nothing about who set it. RichTextFormatterManager.textColorKey is the marker that identifies a run the user deliberately coloured, and it is what the serializer looks for. Always write and remove the two together.
- Swift
Use opaque colours. Only
#rgb and #rrggbb are valid on the wire — eight-digit #rrggbbaa is rejected deliberately, since a fully transparent run would be an invisible message.A translucent UIColor is not rejected locally: it renders in the composer, then is dropped on send, because a colour with alpha below 1 has no wire representation. That is the silent loss this section exists to prevent, so pass a fully opaque colour.Advanced
For advanced-level customization, you can set custom views to the component. This lets you tailor each aspect of the component to fit your exact needs and application aesthetics. You can create and define your views, layouts, and UI elements and then incorporate those into the component.setTextFormatters
Assigns the list of text formatters. If the provided list is not null, it sets the list. Otherwise, it assigns the default text formatters retrieved from the data source. To configure the existing Mentions look and feel check out CometChatMentionsFormatter Example- Swift
setAttachmentOptions
By usingset(attachmentOptions:), you can set a list of custom CometChatMessageComposerAction for the CompactMessageComposer Component. This will override the existing list of CometChatMessageComposerAction.
- Swift
- Swift
Edit & Reply Mode
The CompactMessageComposer supports editing and replying to messages. You can programmatically put the composer into edit or reply mode. Edit a message- Swift
- Swift
- Swift
Complete Integration Example
- Swift