Tob Tob - 1 year ago 133
iOS Question

How do you make the sender display name appear with JSQMessageViewController?

I have the following function that gets called for adding a message:

func addMessage(text: String, displayName: String) {
let message = JSQMessage(senderId: "tester", displayName: displayName, text: text)



Then in this function

override func collectionView(collectionView: JSQMessagesCollectionView!,
messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]

I return the message date for that indexPath. The message appears correctly but there is no display name.

Answer Source

I think you are missing the attributedTextForMessageBubbleTopLabelAtIndexPath should look something like this

 override func collectionView(collectionView: JSQMessagesCollectionView?, attributedTextForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> NSAttributedString! {
    let message = messages[indexPath.item]
    switch message.senderId {
        return nil
        guard let senderDisplayName = message.senderDisplayName else {
            return nil
        return NSAttributedString(string: senderDisplayName)


Good Luck

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download