我正在寻找一种方法来改变气泡中的消息文本颜色,我发现在objc示例中很容易,尝试在swift上做同样的事情,但失败了,有什么解决方案吗?
这是OBJC代码

- (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;
}**

我的快速审判
swift - 更改聊天气泡中的textview颜色-LMLPHP
在根据需要使用override将其编辑为collectionview后,它再次崩溃。
swift - 更改聊天气泡中的textview颜色-LMLPHP

最佳答案

从上面发布的目标C代码中,您需要使用JSQMessagesCollectionViewCell创建super.collectionView(_:cellForRowAt:)实例,因此请尝试这样做。

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

完整解决方案:
 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
    }

关于swift - 更改聊天气泡中的textview颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41346175/

10-11 07:44