> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Mentions Formatter

> Format @mentions in CometChat iOS UI Kit messages with custom styles, mention suggestions, group member mentions, and tap handling.

## Overview

The `CometChatMentionsFormatter` class is part of the CometChat UI Kit, a ready-to-use chat UI component library for integrating CometChat into your iOS applications. This class provides functionality to format mentions within text messages displayed in the chat interface. Mentions allow users to reference other users within a conversation, providing a convenient way to direct messages or involve specific participants.

***

## Features

* **Mention Formatting**: Automatically formats mentions within text messages based on provided styles and settings
* **Customizable Styles**: Allows customization of text styles for mentions, including colors, fonts, and background colors
* **User and Group Member Mentions**: Supports mentions for both individual users and group members, providing flexibility in communication scenarios
* **Mention List Generation**: Generates a list of suggested mentions based on user input, facilitating easy selection of recipients during message composition
* **Mention Click Handling**: Provides a listener interface for handling click events on mentions, enabling custom actions to be performed when a mention is tapped by the user

***

## Usage

To integrate the `CometChatMentionsFormatter` class into your application:

1. **Initialization**: Create an instance of the `CometChatMentionsFormatter` class and configure it with desired settings, such as mention text styles and limit settings.

2. **Set Mention Listeners**: Set listeners for handling mention click events (`.setOnMentionClicked`).

3. **Format Messages**: Use the `.set(leftBubbleTextStyle)`, `.set(rightBubbleTextStyle)`, `.set(composerTextStyle)`, and `.set(conversationListTextStyle)` methods to format text messages with mentions appropriately for display in the chat interface.

4. **Customization**: Customize the appearance and behavior of mentions by adjusting the text styles, colors, and other formatting properties as needed.

`CometChatMentionsFormatter` can be directly initialized, tweaked using its properties, and passed into different configurations.

Below is an example demonstrating how to use the `CometChatMentionsFormatter` class in components such as [CometChatConversations](/ui-kit/ios/conversations), [CometChatMessageList](/ui-kit/ios/message-list), and [CometChatMessageComposer](/ui-kit/ios/message-composer).

<Tabs>
  <Tab title="Swift">
    ```swift lines theme={null}
    // Create a custom mention formatter instance
    let customMentionFormatter = CometChatMentionsFormatter()

    // Apply the formatter to a message list
    let messageListView = CometChatMessageList()
    messageListView.set(textFormatters: [customMentionFormatter])
    ```
  </Tab>
</Tabs>

***

## Actions

[Actions](/ui-kit/ios/components-overview#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.

### setOnMentionClick

Setting a listener for mention-click events in Message Bubbles enhances interactivity within the chat. This listener is activated when a mention is clicked, returning the corresponding user object. This feature transforms mentions into interactive links, enabling more in-depth and contextual engagement with the user associated with the clicked mention.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b/y8uIEcRYKgfQIY6z/images/9e4217e5-mentions_action-8d1b95497f18a60bb85698afde2038d0.png?fit=max&auto=format&n=y8uIEcRYKgfQIY6z&q=85&s=d41a1d0693f35d2d54baf26b87fcb675" width="2560" height="1600" data-path="images/9e4217e5-mentions_action-8d1b95497f18a60bb85698afde2038d0.png" />
</Frame>

<Tabs>
  <Tab title="Swift">
    ```swift lines theme={null}
    // Set up mention click handler
    let customMentionFormatter = CometChatMentionsFormatter()
    customMentionFormatter.set { message, tappedText, controller in
        // Display the name of the first mentioned user
        controller?.view.showToastMessage(message: "\(message.mentionedUsers.first?.name)")
    }
    ```
  </Tab>
</Tabs>

The following code snippet represents a UIView extension used to display `showToastMessage`:

<Tabs>
  <Tab title="Swift">
    ```swift lines theme={null}
    extension UIView {
        func showToastMessage(message: String) {
            // Create toast label with centered positioning
            let toastLabel = UILabel(frame: CGRect(
                x: self.frame.size.width/2 - 100,
                y: self.frame.size.height - 120,
                width: 200,
                height: 35
            ))
            
            // Configure toast appearance
            toastLabel.backgroundColor = UIColor.black
            toastLabel.textColor = UIColor.white
            toastLabel.textAlignment = .center
            toastLabel.text = message
            toastLabel.alpha = 2.0
            toastLabel.layer.cornerRadius = 13
            toastLabel.clipsToBounds = true
            
            self.addSubview(toastLabel)
            
            // Animate fade out and remove
            UIView.animate(withDuration: 4.0, delay: 0.4, options: .curveEaseOut, animations: {
                toastLabel.alpha = 0.0
            }, completion: { isCompleted in
                toastLabel.removeFromSuperview()
            })
        }
    }
    ```
  </Tab>
</Tabs>

***

## Customization

| Method                              | Description                                                 | Type                                                                                        |
| ----------------------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| **Set Conversation List TextStyle** | Sets styling for conversation list's formatted text         | `MentionTextStyle`                                                                          |
| **Set GroupMembersRequestBuilder**  | Sets the builder for fetching group members                 | `GroupMembersRequest.GroupMembersRequestBuilder?`                                           |
| **Set UsersRequestBuilder**         | Sets the builder for fetching users                         | `UsersRequest.UsersRequestBuilder?`                                                         |
| **Set OnMentionClick**              | Sets a listener for mention click events in Message Bubbles | `((_ baseMessage: BaseMessage, _ tappedText: String, _ controller: UIViewController?)->())` |
| **Set composerTextStyle**           | Sets styling for composer's formatted text                  | `MentionTextStyle`                                                                          |

***

## Advanced

For advanced-level customization, you can set the style of the Mentions formatters. This lets you tailor each aspect of the component to fit your exact needs and application aesthetics. You can create and define your own formatter styles.

### Composer Mention Text Style

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 for [CometChatMessageComposer](/ui-kit/ios/message-composer), refer to the code snippet below.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b/iII-209mTdOytBSH/images/f1fa27e6-composer_mention_text-57a6cbaa973eca94248a14c0ef34fa99.png?fit=max&auto=format&n=iII-209mTdOytBSH&q=85&s=d4725219942aa2d78946a0277461f9d2" width="2560" height="1600" data-path="images/f1fa27e6-composer_mention_text-57a6cbaa973eca94248a14c0ef34fa99.png" />
</Frame>

<Tabs>
  <Tab title="Swift">
    ```swift lines theme={null}
    // Create and configure composer mention style
    let composerMentionStyle = MentionTextStyle()
    composerMentionStyle.textColor = UIColor(hex: "#30A46C")

    // Apply the style globally
    CometChatMentionsFormatter.composerTextStyle = composerMentionStyle
    ```
  </Tab>
</Tabs>

### Message Bubble Mention Text Style

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 for [CometChatMessageList](/ui-kit/ios/message-list), refer to the code snippet below.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b/avBd8wan4jblx26s/images/25dbee16-bubble_mention_text-22d8cb8e8b4e85ee2fb2d504a38be93a.png?fit=max&auto=format&n=avBd8wan4jblx26s&q=85&s=3de9ea334f0884933afd5fa89d1c4220" width="2560" height="1600" data-path="images/25dbee16-bubble_mention_text-22d8cb8e8b4e85ee2fb2d504a38be93a.png" />
</Frame>

<Tabs>
  <Tab title="Swift">
    ```swift lines theme={null}
    // Configure left bubble (incoming) mention style
    var leftBubbleMentionsStyle = MentionTextStyle()
    leftBubbleMentionsStyle.textColor = UIColor(hex: "#D6409F")
    CometChatMentionsFormatter.leftBubbleTextStyle = leftBubbleMentionsStyle

    // Configure right bubble (outgoing) mention style
    var rightBubbleMentionsStyle = MentionTextStyle()
    rightBubbleMentionsStyle.textColor = UIColor(hex: "#30A46C")
    CometChatMentionsFormatter.rightBubbleTextStyle = rightBubbleMentionsStyle
    ```
  </Tab>
</Tabs>

### Conversations Mention Text Style

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 for [CometChatConversations](/ui-kit/ios/conversations), refer to the code snippet below.

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b/v4EbHQCGZ54oJcmr/images/58a99728-conversation_mention_text-e0bc3f77a54adcbdac922eb7c2697627.png?fit=max&auto=format&n=v4EbHQCGZ54oJcmr&q=85&s=96994e6aade232330e2ce9a92bcbfd1a" width="2560" height="1600" data-path="images/58a99728-conversation_mention_text-e0bc3f77a54adcbdac922eb7c2697627.png" />
</Frame>

<Tabs>
  <Tab title="Swift">
    ```swift lines theme={null}
    // Create and configure conversation list mention style
    let mentionsStyle = MentionTextStyle()
    mentionsStyle.textColor = UIColor(hex: "#30A46C")

    // Apply the style globally
    CometChatMentionsFormatter.conversationListTextStyle = mentionsStyle
    ```
  </Tab>
</Tabs>

***

## Next Steps

* [Message Composer](/ui-kit/ios/message-composer) — Learn about the message input component
* [Message List](/ui-kit/ios/message-list) — Display and customize chat messages
* [Conversations](/ui-kit/ios/conversations) — Manage conversation lists

***
