Skip to main content

Introduction

The ShortCutFormatter class extends the CometChatTextFormatter class to provide a mechanism for handling shortcuts within messages. This guide will walk 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.
  1. initializer: Sets the trackingCharacter to ’!’ in the initializer of the ShortcutFormatter class
  1. Prepare Regex: Set the regular expression for shortcut detection in the getRegex() method of ShortcutFormatter class.
  1. Prepare TrackingCharacter: Define the getTrackingCharacter() method to return ’!’ as the shortcut tracking character in ShortcutFormatter class.
  1. Override Search Method: Override the search() method to search for shortcuts based on the entered query.
  1. Handle prepareMessageString: Implement the prepareMessageString() method to convert the base chat message into an attributed string for display in the ShortcutFormatter class.
  1. Handle onTextTapped: Override the onTextTapped() method if needed.
  • Below is an example of how a ShortcutFormatter Swift file might look:
ShortcutFormatter.swift

Usage

  1. Initialization: Initialize an instance of ShortCutFormatter in your application.
  1. Integration: Integrating the ShortCutFormatter into your CometChat application involves incorporating it within your project to handle message shortcuts. If you’re utilizing the CometChatConversationsWithMessages component, you can seamlessly integrate the ShortCutFormatter to manage shortcut functionalities within your application.
  • Your final integration code should look like this:
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