2

I'm looking for a way to change messages text color in bubbles, I've found it easy in ObjC example, tried to do the same at swift but failed, any solutions?

Here's the ObjC code

- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     *  Override point for customizing cells
     */
    JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];

    /**
     *  Configure almost *anything* on the cell
     *
     *  Text colors, label text, label colors, etc.
     *
     *
     *  DO NOT set `cell.textView.font` !
     *  Instead, you need to set `self.collectionView.collectionViewLayout.messageBubbleFont` to the font you want in `viewDidLoad`
     *
     *
     *  DO NOT manipulate cell layout information!
     *  Instead, override the properties you want on `self.collectionView.collectionViewLayout` from `viewDidLoad`
     */

    JSQMessage *msg = [self.demoData.messages objectAtIndex:indexPath.item];

    if (!msg.isMediaMessage) {

        if ([msg.senderId isEqualToString:self.senderId]) {
            cell.textView.textColor = [UIColor blackColor];
        }
        else {
            cell.textView.textColor = [UIColor whiteColor];
        }

        cell.textView.linkTextAttributes = @{ NSForegroundColorAttributeName : cell.textView.textColor,
                                              NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid) };
    }
    cell.accessoryButton.hidden = ![self shouldShowAccessoryButtonForMessage:msg];

    return cell;
}**

My swift trial Swift version

After editing it to collectionview with override as it required, it crashes again

enter image description here

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Abdoelrhman
  • 906
  • 8
  • 17

1 Answers1

4

From the Objective c code that you have posted above you need to create JSQMessagesCollectionViewCell instance using super.collectionView(_:cellForRowAt:), so try like this.

let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

full solution:

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

        // messages to show
        let msg = incomingMessages[indexPath.row]

        if !msg.isMediaMessage {
            if msg.senderId! == senderId {
                cell.textView.textColor = UIColor.white
            }else{
                cell.textView.textColor = UIColor.black
            }
            cell.textView.linkTextAttributes = [NSForegroundColorAttributeName: cell.textView.textColor ?? UIColor.white]
        }
        return cell 
    }
Abdoelrhman
  • 906
  • 8
  • 17
Nirav D
  • 71,513
  • 12
  • 161
  • 183