Search code examples
swiftjsqmessagesviewcontroller

Attribute different color to each user in Group chat in JSQMessagesViewController?


Im using JSQMessagesViewController for my chat App and I want to attribute to each user name a different color

In my createAvatar function , Im assigning different color to my ColorForEachUser dictionnary :

    var avatars = [String:JSQMessagesAvatarImage]()
    var colorsForEachUser = [String: UIColor]()

    func createAvatar(senderID: String, senderDisplayName:String , senderPhoneNumber : String, color:UIColor){
            if avatars[senderID] == nil{

                //MARK: - load the user image in avatar
                let profileImageName = "\(senderPhoneNumber)_small.jpg"
                let profileImagePath = fileInDocumentsDirectory(profileImageName)

                if let loadedImage = loadImageFromPath(profileImagePath){
                    print(" Loaded Image from document directory: \(loadedImage)")
                    let avatar = JSQMessagesAvatarImageFactory.avatarImageWithImage(loadedImage, diameter: 70)
                    avatars[senderID] = avatar
                }else{
                    print("not found in document directory")
                    let avatar = JSQMessagesAvatarImageFactory.avatarImageWithImage(UIImage(named:"defaultUser.png" ), diameter: 70)
                    avatars[senderID] = avatar

                }
                //MARK: - load the user color in avatar name
                let colors = [TwitterBlue , pinkIlana , facebookBlue , favoritesRedColor, orange, purple , greenFluo, greenOlive ,yellowFluo, pinkFluo, redFull, brown, greenLight, blueBlack]
// POPULATING MY DICTIONNARY
                colorsForEachUser[senderID] = colors[Int(arc4random_uniform(13))]
            }
     }

and Im using that in my attributedTextForMessageBubbleTopLabelAtIndexPath Function:

   //MARK: text top of the bubble
        override func collectionView(collectionView: JSQMessagesCollectionView?, attributedTextForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> NSAttributedString! {
            let message = messages[indexPath.item]
            switch message.senderId {
            case senderId:
                return nil
            default:
                guard let senderDisplayName = message.senderDisplayName else {
                    assertionFailure()
                    return nil
                }
                let range = (senderDisplayName as NSString).rangeOfString(senderDisplayName)
//HERE I TRY TO USE DIFFERENT COLORS 
                let attributedString = 
NSMutableAttributedString(string:senderDisplayName)
                attributedString.addAttribute(NSForegroundColorAttributeName, value: colorsForEachUser[senderId]! , range: range)

                return attributedString
            }
        }

but I get THE SAME RANDOM color for users that are not me.. Thanks for your help!


Solution

  • my bad ! JUST HAVE TO CHANGE senderId to message.senderId in colorsForEachUser[senderId] when attributing