Skip to main content
Version: v4

Message Composer

Overview

MessageComposer is a Component that enables users to write and send a variety of messages, including text, image, video, and custom messages.

Features such as Live Reaction, Attachments, and Message Editing are also supported by it.

Image

MessageComposer is comprised of the following Base Components:

Base ComponentsDescription
MessageInputThis provides a basic layout for the contents of this component, such as the TextField and buttons
ActionSheetThe ActionSheet component presents a list of options in either a list or grid mode, depending on the user's preference

Usage

Integration

The following code snippet illustrates how you can directly incorporate the MessageComposer component into your file.

// syntax for set(user: User)
let messageComposer = CometChatMessageComposer()
messageComposer.set(user: user)
messageComposer.set(parentMessageId: 20)

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.

1. OnSendButtonClick

The OnSendButtonClick event gets activated when the send message button is clicked. The following code snippet Overrides the action of the send button in CometChatMessageComposer.

// syntax for set(sendIcon: UIImage) 
messageComposer.setOnSendButtonClick { message in
// return custom action here
}

Filters

MessageComposer 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 MessageComposer Component does not emit any events of its own.


Customization

To fit your app's design requirements, you can customize the appearance of the MessageComposer 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.

1. MessageComposer Style

To customize the styling, you can apply the MessageComposerStyle to the MessageComposer component.

let messageComposerStyle  = MessageComposerStyle()
let messageComposerConfiguration = MessageComposerConfiguration()
.set(messageComposerStyle: messageComposerStyle)

The following properties are exposed by MessageComposerStyle:

PropertyDescriptionMethod
Set BorderWidthUsed to set outermost border.set(borderWidth: CGFloat)
Set BorderColorUsed to set border color.set(borderColor: UIColor)
Set CornerRadiusUsed to set corner radius.set(cornerRadius: CometChatCornerStyle)
Set BackgroundThis method will set the background color for message list.set(background: UIColor)
Set InputBackgroundColorUsed to set input background color.set(inputBackground: UIColor)
Set TextAppearanceUsed to set input text style.set(textFont: UIFont)
Set InputBoxPlaceholderFontUsed to set placeholder font for message composer.set(inputBoxPlaceholderFont: UIFont)
Set PlaceHolderTextColorUsed to set placeholder text color.set(placeHolderTextColor: UIColor)
Set AttachIconTintUsed to set attachment icon tint.set(attachmentIconTint: UIColor)
Set SendIconTintUsed to set send button icon tint.set(sendIconTint: UIColor)
Set DividerTintUsed to set separator color.set(dividerTint: UIColor)
Set InputBorderWidthUsed to set input border width for message composer.set(inputBorderWidth: CGFloat)
Set InputBorderColorUsed to set input border color for message composer.set(inputBorderColor: UIColor)
Set ActionSheetTitleColorUsed to set action sheet title color for message composer.set(actionSheetTitleColor: UIColor)
Set ActionSheetTitleFontUsed to set action sheet title font for message composer.set(actionSheetTitleFont: UIFont)
Set ActionSheetLayoutModelIconTintSets action sheet layout mode icon tint color for message composer.set(actionSheetLayoutModelIconTint: UIColor)
Set ActionSheetCancelButtonIconTintSets action sheet cancel button icon tint color for message composer.set(actionSheetCancelButtonIconTint: UIColor)
Set ActionSheetCancelButtonIconFontSets the action sheet cancel button icon font color for message composer.set(actionSheetCancelButtonIconFont: UIFont)
2. MediaRecorder Style report

To customize the styles of the MediaRecorder component within the MessageComposer Component, use the .setMediaRecorderStyle() method. For more details, please refer to MediaRecorder styles.

let mediarecorderStyle = MediaRecorderStyle()
.set(borderColor: .orange)
.set(borderWidth: 10)

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.

let messageComposer  = CometChatMessageComposer()
.disable(disableTypingEvents: true)
.hide(liveReaction: true)

Below is a list of customizations along with corresponding code snippets

PropertyDescriptionCode
User report Used to pass user object of which header specific details will be shown.set(user: User)
Group report Used to pass group object of which header specific details will be shown.set(group: Group)
Set PlaceHolderTextUsed to set composer's placeholder text.set(placeholderText: String)
Disable TypingEventsUsed to disable/enable typing events , default false.disable(disableTypingEvents: Bool)
Disable SoundForMessagesUsed to toggle sound for outgoing messages.disable(disableSoundForMessages: Bool)
Set MaxLineMaximum lines allowed to increase in the input field.set(maxLines: Int)
Set AuxiliaryButtonAlignmentcontrols position auxiliary button view , can be left or right . default right.set(auxiliaryButtonAlignment: AuxiliaryButtonAlignment)
Hide LiveReactionused to toggle visibility for live reaction component.hide(liveReaction: Bool)
Set CustomSoundForMessagesUsed to give custom sounds to outgoing messages.set(customSoundForMessage: URL)
Set LiveReactionIconused to set custom live reaction icon..set(LiveReactionIconURL: UIImage)
Set Parent MessageidSets the parentMessageId used in the CometChatMessageComposer for all sub-components. It is use to send messages to the particular thread..set(parentMessageId: Int)

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

Image

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 messageComposerConfiguration = MessageComposerConfiguration()
.set(textFormatter: [customMentionFormatter])

let cometChatMessages = CometChatMessages()
.set(user: user)
.set(messageComposerConfiguration: messageComposerConfiguration)

AttachmentOptions

By using setAttachmentOptions(), you can set a list of custom MessageComposerActions for the MessageComposer Component. This will override the existing list of MessageComposerActions.

 let messageComposerConfiguration = MessageComposerConfiguration()
.setAttachmentOptions { user, group, controller in
}

Example

Image

In this example, we are overriding the existing MessageComposerActions List with Capture Photo actions.

let messageComposerConfiguration = MessageComposerConfiguration()
.setAttachmentOptions { user, group, controller in

let customComposerAction1 = CometChatMessageComposerAction(
id: "customAction1",
text: "Capture",
startIcon: UIImage(systemName: "camera"),
endIcon: nil,
startIconTint: .blue,
endIconTint: nil,
textColor: .purple,
textFont: .italicSystemFont(ofSize: 15),
backgroundColour: .white,
borderWidth: 1,
borderColor: .gray,
onActionClick: {
print("Custom Action 1 clicked!")
}
)

return [customComposerAction1 , customComposerAction1, customComposerAction1, customComposerAction1 ]
}

let cometChatMessages = CometChatMessages()
.set(user: user)
.set(messageComposerConfiguration: messageComposerConfiguration)

AuxiliaryButtonView

You can insert a custom view into the MessageComposer component to add additional functionality using the following method.

let messageComposerConfiguration = MessageComposerConfiguration()
.setAuxilaryButtonView { user, group in

}

Please note that the MessageComposer Component utilizes the AuxiliaryButton to provide sticker functionality. Overriding the AuxiliaryButton will subsequently replace the sticker functionality.

Example

In this example, we'll be adding a custom button with click functionality. You'll first need to create a custom UIView file and then inflate it inside the .setAuxiliaryButtonView() function.

Swift
let messageComposerConfiguration = MessageComposerConfiguration()
.setAuxilaryButtonView { user, group in
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false

let customButton = UIButton()
customButton.setImage(UIImage(systemName: "bell"), for: .normal)
customButton.translatesAutoresizingMaskIntoConstraints = false
customButton.addTarget(self, action: #selector(self.customButtonClicked), for: .touchUpInside)

customView.addSubview(customButton)

NSLayoutConstraint.activate([
customButton.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -8),
customButton.centerYAnchor.constraint(equalTo: customView.centerYAnchor),
customButton.heightAnchor.constraint(equalToConstant: 30),
customButton.widthAnchor.constraint(equalToConstant: 80)
])

return customView

}

let cometChatMessages = CometChatMessages()
.set(user: user)
.set(messageComposerConfiguration: messageComposerConfiguration)

SecondaryButtonView

You can add a custom view into the SecondaryButton component for additional functionality using the below method.

let messageComposerConfiguration = MessageComposerConfiguration()
.setSecondaryButtonView { user, group in
}

Please note that the MessageComposer Component uses the SecondaryButton to open the ComposerActionsList. Overriding the SecondaryButton will replace the ComposerActionsList functionality.

Example

Image

In this example, we'll be adding a custom button with click functionality. You'll first need to create a custom UIView file and then inflate it inside the .setSecondaryButtonView() function.

swift
let messageComposerConfiguration = MessageComposerConfiguration()
.setSecondaryButtonView { user, group in
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false

let customButton = UIButton()
customButton.setImage(UIImage(systemName: "bell"), for: .normal)
customButton.translatesAutoresizingMaskIntoConstraints = false
customButton.addTarget(self, action: #selector(self.customButtonClicked), for: .touchUpInside)

customView.addSubview(customButton)

NSLayoutConstraint.activate([
customButton.leadingAnchor.constraint(equalTo: customView.leadingAnchor, constant: -20),
customButton.centerYAnchor.constraint(equalTo: customView.centerYAnchor),
customButton.heightAnchor.constraint(equalToConstant: 30),
customButton.widthAnchor.constraint(equalToConstant: 80)
])

return customView
}
let cometChatMessages = CometChatMessages()
.set(user: user)
.set(messageComposerConfiguration: messageComposerConfiguration)

Set SendButtonView

You can set a custom view in place of the already existing send button view. Using the following method.

 let messageComposerConfiguration = MessageComposerConfiguration()
.setSendButtonView { user, group in
}

Example

let messageComposerConfiguration = MessageComposerConfiguration()
.setSendButtonView { user, group in
let customButton = UIButton()
customButton.setImage(UIImage(systemName: "bell"), for: .normal)
customButton.translatesAutoresizingMaskIntoConstraints = false
customButton.addTarget(self, action: #selector(self.customButtonClicked), for: .touchUpInside)
return customButton
}
let cometChatMessages = CometChatMessages()
.set(user: user)
.set(messageComposerConfiguration: messageComposerConfiguration)


HeaderView

You can set custom headerView to the MessageComposer component using the following method

let messageComposerConfiguration = MessageComposerConfiguration()
.set(headerView: UIView())

Example

Image

In the following example, we're going to apply a mock smart reply view to the MessageComposer Component using the .set(headerView: UIView?) method.

Custom_View_file
import UIKit

class CustomActionView: UIView {

let actionButton: UIButton

override init(frame: CGRect) {
actionButton = UIButton(type: .system)
actionButton.backgroundColor? = .purple
actionButton.tintColor = .init(red: 0.41, green: 0.32, blue: 0.84, alpha: 1.00)

actionButton.setTitle("Smart Replies", for: .normal)
let image = UIImage(systemName: "arrowshape.turn.up.left.2.fill")
actionButton.setImage(image, for: .normal)
actionButton.translatesAutoresizingMaskIntoConstraints = false

super.init(frame: frame)

addSubview(actionButton)
setupButtonConstraints()

actionButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupButtonConstraints() {
NSLayoutConstraint.activate([
actionButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8),
actionButton.centerYAnchor.constraint(equalTo: self.centerYAnchor)
])
}

@objc private func buttonTapped() {
print("Button was tapped!")
}
}

 let messageComposerConfiguration = MessageComposerConfiguration()
.set(headerView: CustomActionView())

let cometChatMessages = CometChatMessages()
.set(user: user)
.set(messageComposerConfiguration: messageComposerConfiguration)

info

Ensure to pass and present cometChatMessages. If a navigation controller is already in use, utilize the pushViewController function instead of directly presenting the view controller.


FooterView

You can set a custom footer view to the MessageComposer component using the following method:

 let messageComposerConfiguration = MessageComposerConfiguration()
.set(footerView: UIView())

Example

Image

In the following example, we're going to apply a mock smart reply view to the MessageComposer Component using the .set(footerView: UIView?) method.

Custom_View_file
import UIKit

class CustomActionView: UIView {

let actionButton: UIButton

override init(frame: CGRect) {
actionButton = UIButton(type: .system)
actionButton.backgroundColor? = .purple
actionButton.tintColor = .init(red: 0.41, green: 0.32, blue: 0.84, alpha: 1.00)

actionButton.setTitle("Smart Replies", for: .normal)

let image = UIImage(systemName: "repeat")
actionButton.setImage(image, for: .normal)
actionButton.translatesAutoresizingMaskIntoConstraints = false

super.init(frame: frame)

addSubview(actionButton)
setupButtonConstraints()

actionButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupButtonConstraints() {
NSLayoutConstraint.activate([
actionButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8),
actionButton.centerYAnchor.constraint(equalTo: self.centerYAnchor)
])
}

@objc private func buttonTapped() {
print("Button was tapped!")
}
}
let messageComposerConfiguration = MessageComposerConfiguration()
.set(footerView: CustomActionView())

let cometChatMessages = CometChatMessages()
.set(user: user)
.set(messageComposerConfiguration: messageComposerConfiguration)
info

Ensure to pass and present cometChatMessages. If a navigation controller is already in use, utilize the pushViewController function instead of directly presenting the view controller.