1

I'm struggling to find a way to display the username in the MessageKit chat view controller. I copied the MessagesDataSource from the example project with no luck. Here's what my MessagesDataSource currently looks like:

extension ChatViewController: MessagesDataSource {

    func currentSender() -> Sender {
        //guard let currentUserID = User.current?.key else {return nil}
        let newSender = Sender(id: (User.current?.key)!, displayName: (User.current?.username)!)
        return newSender
    }

    func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
        //return 1
        return messages.count
    }

    func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {

        return messages[indexPath.section]
    }

    func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {

        return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.darkGray])
    }

    func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
        let name = message.sender.displayName
        return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
    }

    func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {

        let dateString = formatter.string(from: message.sentDate)
        return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
    }
}

If I place a breakpoint after let name = message.sender.displayName the proper username is displayed in the console output. However, it still doesn't appear in the chat history. The chat view still only displays the avatar and the message, nothing else.

What am I missing?

Thanks

winston
  • 3,000
  • 11
  • 44
  • 75

2 Answers2

3

Altho i don't know what, version of MessageKit you use, but i am pretty sure if you tried this everything should work just fine,

func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
    let name = message.sender.displayName
    return NSAttributedString(
      string: name,
      attributes: [
        .font: UIFont.preferredFont(forTextStyle: .caption1),
        .foregroundColor: UIColor(white: 0.3, alpha: 1)
      ]
    )
  }

As i don't see any dataSource function that's called messageTopLabelAttributedText

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • thanks for the reply! Unfortunately this still didn't change anything. Is there a global setting I might be missing? – winston Mar 03 '19 at 03:10
  • I figured it out! Looks like I also needed `cellTopLabelHeight` function to go along with your answer. Looks like the display name is centered, even though the messages are aligned to the left or right. Do you know how to fix that? Thanks again for the help – winston Mar 03 '19 at 03:21
  • 1
    by the way for anyone wondering, I forgot to mention that the `cellTopLabelHeight` goes in the MessagesLayoutDelegate section – winston Mar 03 '19 at 03:21
  • 1
    Figured out the text alignment! Just needed `let paragraph = NSMutableParagraphStyle()` + `.paragraphStyle: paragraph` – winston Mar 03 '19 at 04:17
3

Please add this code bellow:

func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
    return 35
}
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68