Skip to main content

Introduction

The ShortCutFormatter class extends the CometChatTextFormatter class to provide a mechanism for handling shortcuts within messages. This guide walks you through the process of using ShortCutFormatter to implement shortcut extensions in your CometChat application.

Setup

1. Create the ShortCutFormatter Class

Define the ShortCutFormatter class by extending the CometChatTextFormatter class:

2. Initialize with Tracking Character

Set the trackingCharacter to '!' in the initializer:

3. Define the Regex Pattern

Set the regular expression for shortcut detection in the getRegex() method:

4. Return the Tracking Character

Define the getTrackingCharacter() method to return '!' as the shortcut tracking character:

5. Implement the Search Method

Override the search() method to search for shortcuts based on the entered query:

6. Handle Message String Preparation

Implement the prepareMessageString() method to convert the base chat message into an attributed string for display:

7. Handle Text Tap Events

Override the onTextTapped() method if you need to handle tap events on formatted text:

Complete Implementation

Here’s the complete ShortcutFormatter class:

Usage

1. Initialize the Formatter

Create an instance of ShortCutFormatter:

2. Integrate with Message Composer

If you’re using the CometChatMessageComposer component, integrate the ShortCutFormatter to manage shortcut functionalities:
Ensure to pass and present cometChatConversationsWithMessages. If a navigation controller is already in use, utilize the pushViewController function instead of directly presenting the view controller.

Example


Next Steps

Mentions Formatter

Add @mentions with styled tokens and click handling.

Message Composer

Customize the message input component.

Message List

Display and customize chat messages.

Message Template

Define custom message bubble structures.