Skip to main content

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.
let compactComposer = CometChatCompactMessageComposer()
compactComposer.set(user: user)
compactComposer.set(controller: self)
compactComposer.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(compactComposer)
To ensure that the CometChatCompactMessageComposer is properly configured, passing the controller is mandatory.
compactComposer.set(controller: self) // Passing the controller is required

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
The set(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.
compactComposer.set(onSendButtonClick: { message in
    // Your custom send handling
})

setOnError
This action doesn’t change the behavior of the component but rather listens for any errors that occur in the CompactMessageComposer component.
compactComposer.set(onError: { error in
    // Your Exception Handling code
})

setOnTextChangedListener
Function triggered whenever the message input’s text value changes, enabling dynamic text handling.
compactComposer.set(onTextChangedListener: { text in
    // Handle text change
})

setAttachmentOptions
This action is triggered when the attachment button is clicked. You can override the default attachment sheet behavior with a custom implementation.
compactComposer.set(attachmentOptions: { user, group, controller in
    // Return custom attachment actions
    return [CometChatMessageComposerAction]
})

Filters

CompactMessageComposer component does not have any available filters.

Events

Events are emitted by a Component. 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
CometChatCompactMessageComposer.style.activeSendButtonBackgroundColor = UIColor(hex: "#F76808")
CometChatCompactMessageComposer.style.attachmentImageTint = UIColor(hex: "#F76808")
CometChatCompactMessageComposer.style.voiceRecordingImageTint = UIColor(hex: "#F76808")
CometChatCompactMessageComposer.style.stickersImageTint = UIColor(hex: "#F76808")
Instance level styling
var customStyle = CompactMessageComposerStyle()
customStyle.activeSendButtonBackgroundColor = UIColor(hex: "#F76808")
customStyle.attachmentImageTint = UIColor(hex: "#F76808")
customStyle.voiceRecordingImageTint = UIColor(hex: "#F76808")
customStyle.stickersImageTint = UIColor(hex: "#F76808")

let compactComposer = CometChatCompactMessageComposer()
compactComposer.style = customStyle
The following properties are exposed by CompactMessageComposerStyle:
PropertyDescriptionCode
backgroundColorBackground color of the composer container.style.backgroundColor = CometChatTheme.backgroundColor03
cornerRadiusCorner radius for the composer container.style.cornerRadius = CometChatCornerStyle?
borderWidthBorder width for the composer container.style.borderWidth = 0
borderColorBorder color for the composer container.style.borderColor = .clear
composeBoxBackgroundColorBackground color for the compose box.style.composeBoxBackgroundColor = CometChatTheme.backgroundColor01
composeBoxBorderColorBorder color for the compose box.style.composeBoxBorderColor = CometChatTheme.borderColorDefault
composeBoxBorderWidthBorder width for the compose box.style.composeBoxBorderWidth = 1
composeBoxCornerRadiusCorner radius for the compose box.style.composeBoxCornerRadius = .init(cornerRadius: CometChatSpacing.Radius.r2)
composerSeparatorColorColor for the separator in the compose box.style.composerSeparatorColor = CometChatTheme.borderColorLight
textFieldFontFont for the input text field.style.textFieldFont = CometChatTypography.Body.regular
textFieldColorText color for the input field.style.textFieldColor = CometChatTheme.textColorPrimary
placeholderFontFont for the placeholder text.style.placeholderFont = CometChatTypography.Body.regular
placeholderColorColor for the placeholder text.style.placeholderColor = CometChatTheme.textColorTertiary
sendButtonImageIcon for the send button.style.sendButtonImage = UIImage(named: "custom-send")
sendButtonImageTintTint color for the send button image.style.sendButtonImageTint = CometChatTheme.white
activeSendButtonBackgroundColorBackground color for send button when active.style.activeSendButtonBackgroundColor = CometChatTheme.primaryColor
inactiveSendButtonBackgroundColorBackground color for send button when inactive.style.inactiveSendButtonBackgroundColor = CometChatTheme.neutralColor300
attachmentImageIcon for the attachment button.style.attachmentImage = UIImage(systemName: "plus.circle")
attachmentImageTintTint color for the attachment image.style.attachmentImageTint = CometChatTheme.iconColorSecondary
voiceRecordingImageIcon for the voice recording button.style.voiceRecordingImage = UIImage(systemName: "mic")
voiceRecordingImageTintTint color for the voice recording image.style.voiceRecordingImageTint = CometChatTheme.iconColorSecondary
stickersImageIcon for the stickers button.style.stickersImage = UIImage(named: "sticker-image")
stickersImageTintTint color for the stickers image.style.stickersImageTint = CometChatTheme.iconColorSecondary
stickersActiveImageTintTint color for the stickers image when active.style.stickersActiveImageTint = CometChatTheme.primaryColor
codeBlockBackgroundColorBackground color for code block mode.style.codeBlockBackgroundColor = UIColor(hex: "#F5F5F5")
codeBlockBorderColorBorder color for code block mode.style.codeBlockBorderColor = UIColor(hex: "#E8E8E8")
codeBlockBorderWidthBorder width for code block mode.style.codeBlockBorderWidth = 1
previewTitleFontFont for the title in the edit/reply preview.style.previewTitleFont = CometChatTypography.Body.regular
previewMessageFontFont for the message text in the edit/reply preview.style.previewMessageFont = CometChatTypography.Caption1.regular
previewTitleColorText color for the title in the edit/reply preview.style.previewTitleColor = CometChatTheme.textColorPrimary
previewMessageColorText color for the message in the edit/reply preview.style.previewMessageColor = CometChatTheme.textColorSecondary
previewBackgroundColorBackground color for the edit/reply preview.style.previewBackgroundColor = CometChatTheme.backgroundColor03
previewCloseIconIcon for closing the edit/reply preview.style.previewCloseIcon = UIImage(systemName: "xmark")
previewCloseIconTintTint color for the close icon in the preview.style.previewCloseIconTint = CometChatTheme.iconColorHighlight
infoIconIcon for the mention limit info banner.style.infoIcon = UIImage(systemName: "info.circle")
infoIconTintTint color for the info icon.style.infoIconTint = CometChatTheme.errorColor
infoTextColorText color for the info text.style.infoTextColor = CometChatTheme.errorColor
infoTextFontFont for the info text.style.infoTextFont = CometChatTypography.Caption1.regular
infoBackgroundColorBackground color for the info section.style.infoBackgroundColor = CometChatTheme.backgroundColor02
richTextToolbarStyleStyle configuration for the rich text toolbar.style.richTextToolbarStyle = RichTextToolbarStyle()
agenticSendButtonImageIcon for the send button in agentic mode.style.agenticSendButtonImage = UIImage(systemName: "arrow.up")
agenticSendButtonImageTintTint color for the agentic send button image.style.agenticSendButtonImageTint = CometChatTheme.neutralColor50
agenticActiveSendButtonBackgroundColorBackground color for agentic send button when active.style.agenticActiveSendButtonBackgroundColor = CometChatTheme.neutralColor900
agenticInactiveSendButtonBackgroundColorBackground color for agentic send button when inactive.style.agenticInactiveSendButtonBackgroundColor = CometChatTheme.neutralColor300

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:
PropertyDescriptionCode
UserUsed to pass user object of which header specific details will be shown.set(user: User)
GroupUsed to pass group object of which header specific details will be shown.set(group: Group)
setParentMessageIdSets the parent message ID for threaded replies.set(parentMessageId: Int)
setPlaceholderSets the placeholder text for the input field.set(placeholder: "Type a message...")
setMaxLinesMaximum lines allowed to increase in the input field.set(maxLines: Int)
setTextFormatterAssigns the list of text formatters (e.g., mentions).set(textFormatter: [CometChatTextFormatter])
setEnableRichTextFormattingMaster switch to enable or disable rich text formatting (bold, italic, etc.). Default true.enableRichTextFormatting = true
setRichTextFormattingOptionsVisibilityControls the visibility of the rich text formatting toolbar. Default true.showRichTextFormattingOptions = true
setDisableTypingEventsUsed to disable/enable typing events, default false.disable(typingEvents: true)
setDisableSoundForMessagesUsed to toggle sound for outgoing messages.disable(soundForMessages: true)
setCustomSoundForMessagesUsed to give custom sounds to outgoing messages.customSoundForMessage = URL
setDisableMentionsSets whether mentions in text should be disabled. Removes any formatters that are instances of CometChatMentionsFormatter.disable(mentions: true)
setAttachmentButtonVisibilityHides the attachment button in the composer.hideAttachmentButton = true
setVoiceRecordingButtonVisibilityHides the voice recording button in the composer.hideVoiceRecordingButton = true
setStickerButtonVisibilityHides the sticker button in the composer.hideStickersButton = true
setSendButtonVisibilityHides the send button in the composer.hideSendButton = true

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 to false, 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

FormatDescriptionMarkdown Syntax
BoldMakes text bold**text**
ItalicMakes text italic*text*
UnderlineUnderlines text<u>text</u>
StrikethroughStrikes through text~~text~~
Inline CodeFormats as inline code`code`
Code BlockCreates a code block```code```
BlockquoteCreates a blockquote> text
Numbered ListCreates a numbered list1. item
Bullet ListCreates a bullet list- item
LinkCreates a hyperlink[text](url)

Configuration

// Disable rich text formatting
compactComposer.enableRichTextFormatting = false

// Hide the formatting toolbar
compactComposer.showRichTextFormattingOptions = false
enableRichTextFormattingshowRichTextFormattingOptionsResult
true (default)true (default)Toolbar visible below input, full formatting support
truefalseNo toolbar, markdown syntax is still parsed on send
falsePlain text, no formatting

RichTextToolbarStyle

Global level styling
CometChatCompactMessageComposer.style.richTextToolbarStyle.backgroundColor = UIColor(hex: "#F5F5F5")
CometChatCompactMessageComposer.style.richTextToolbarStyle.iconTintColor = UIColor(hex: "#666666")
CometChatCompactMessageComposer.style.richTextToolbarStyle.activeIconTintColor = UIColor(hex: "#F76808")
CometChatCompactMessageComposer.style.richTextToolbarStyle.activeButtonBackgroundColor = UIColor(hex: "#FFF0E6")
The following properties are exposed by RichTextToolbarStyle:
PropertyDescriptionCode
backgroundColorBackground color of the toolbar.toolbarStyle.backgroundColor = CometChatTheme.backgroundColor02
borderColorBorder color of the toolbar.toolbarStyle.borderColor = CometChatTheme.borderColorLight
buttonSizeSize of format buttons.toolbarStyle.buttonSize = 30
buttonSpacingSpacing between format buttons.toolbarStyle.buttonSpacing = CometChatSpacing.Spacing.s2
iconTintColorTint color for inactive format icons.toolbarStyle.iconTintColor = CometChatTheme.iconColorSecondary
activeIconTintColorTint color for active format icons.toolbarStyle.activeIconTintColor = CometChatTheme.primaryColor
buttonBackgroundColorBackground color for inactive buttons.toolbarStyle.buttonBackgroundColor = .clear
activeButtonBackgroundColorBackground color for active buttons.toolbarStyle.activeButtonBackgroundColor = CometChatTheme.extendedPrimaryColor100

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
let composerTextStyle = MentionTextStyle()
    .set(textBackgroundColor: .white)
    .set(textColor: UIColor.black)
    .set(textFont: UIFont.systemFont(ofSize: 18, weight: .heavy))
    .set(loggedInUserTextColor: UIColor.systemTeal)
    .set(loggedInUserTextFont: UIFont.systemFont(ofSize: 18, weight: .bold))

let customMentionFormatter = CometChatMentionsFormatter()
    .set(composerTextStyle: composerTextStyle)

let compactComposer = CometChatCompactMessageComposer()
compactComposer.set(textFormatter: [customMentionFormatter])

setAttachmentOptions

By using set(attachmentOptions:), you can set a list of custom CometChatMessageComposerAction for the CompactMessageComposer Component. This will override the existing list of CometChatMessageComposerAction.
compactComposer.set(attachmentOptions: { user, group, controller in
    return [CometChatMessageComposerAction]
})
Example In this example, we are overriding the existing MessageComposerActions List with custom actions.
let compactComposer = CometChatCompactMessageComposer()

compactComposer.set(attachmentOptions: { user, group, controller in
    let action1 = CometChatMessageComposerAction(
        id: "customAction1",
        text: "Custom Option 1",
        startIcon: UIImage(systemName: "camera"),
        startIconTint: .black,
        textColor: .black,
        onActionClick: {
            print("Custom Option 1 clicked!")
        }
    )

    let action2 = CometChatMessageComposerAction(
        id: "customAction2",
        text: "Custom Option 2",
        startIcon: UIImage(systemName: "doc"),
        startIconTint: .black,
        textColor: .black,
        onActionClick: {
            print("Custom Option 2 clicked!")
        }
    )

    return [action1, action2]
})

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
compactComposer.edit(message: textMessage)
Reply to a message
compactComposer.reply(message: baseMessage)
Preview a message with a specific mode
compactComposer.preview(message: baseMessage, mode: .edit)
// or
compactComposer.preview(message: baseMessage, mode: .reply)

Complete Integration Example

import UIKit
import CometChatUIKitSwift

class ChatViewController: UIViewController {
    var user: User?

    override func viewDidLoad() {
        super.viewDidLoad()
        setupComposer()
    }

    private func setupComposer() {
        guard let user = user else { return }

        let compactComposer = CometChatCompactMessageComposer()
        compactComposer.set(user: user)
        compactComposer.set(controller: self)

        // Customize
        compactComposer.set(placeholder: "Type a message...")
        compactComposer.disable(typingEvents: false)

        // Callbacks
        compactComposer.set(onSendButtonClick: { message in
            print("Message sent: \(message)")
        })

        compactComposer.set(onError: { error in
            print("Error: \(error.errorDescription)")
        })

        // Layout
        compactComposer.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(compactComposer)

        NSLayoutConstraint.activate([
            compactComposer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            compactComposer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            compactComposer.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        ])
    }
}