在 JSQMessagesViewController 的群聊中为每个用户分配不同的颜色?

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

我正在为我的聊天应用程序使用 JSQMessagesViewController,我想为每个用户名赋予不同的颜色

在我的 createAvatar 函数中,我为我的 ColorForEachUser 字典分配了不同的颜色:

    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))]
            }
     }

我在我的 attributedTextForMessageBubbleTopLabelAtIndexPath 函数中使用它:

   //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
            }
        }

但我为不是我的用户获得了相同的随机颜色.. 谢谢你的帮助!

我的错! 只需要在 colorsForEachUser[senderId] 中将 senderId 更改为 message.senderId when attributing