消息线程`MessageKit`swift的左对齐

Posted

技术标签:

【中文标题】消息线程`MessageKit`swift的左对齐【英文标题】:Left alignment for message threads `MessageKit` swift 【发布时间】:2019-06-30 14:37:56 【问题描述】:

我正在使用MessageKit 库实现一个简单的聊天屏幕。正如您在第一个屏幕截图中看到的,一切听起来都很正常,但我被要求做的是将所有线程向左对齐(第二个屏幕截图)。我试图找到一种方法来改变线程对齐,但我做不到。如果有人可以帮助我找到方法,我将不胜感激。

这是我的代码的一部分:

extension ConversationDataSource: MessagesDisplayDelegate 

    // MARK: - MessagesDisplayDelegate implementation

    func messageStyle(for message: MessageType, at indexPath: IndexPath,
                      in messagesCollectionView: MessagesCollectionView) -> MessageStyle 
        return .bubbleTail(isFromCurrentSender(message: message) ? .topRight : .topLeft, .curved)
    

    func backgroundColor(for message: MessageType, at  indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor 
        return isFromCurrentSender(message: message) ? .aeroBlue : .white
    

    func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor 
        return .black
    

    func enabledDetectors(for message: MessageType, at indexPath: IndexPath,
                          in messagesCollectionView: MessagesCollectionView) -> [DetectorType] 
        return [.url, .address, .phoneNumber, .date]
    

    func detectorAttributes(for detector: DetectorType, and message: MessageType,
                            at indexPath: IndexPath) -> [NSAttributedString.Key: Any] 
        // Project doesn't build with Xcode 10 without implementing this explicitly
        return [NSAttributedString.Key.foregroundColor: UIColor.darkText,
                NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
                NSAttributedString.Key.underlineColor: UIColor.darkText]
    

PS:请不要介意设计,对齐是现在的问题

【问题讨论】:

【参考方案1】:

-> 在 MessagesDataSource 中创建一个函数

    public extension MessagesDataSource 
        func isFromUser1Custom(message: MessageType) -> Bool 
            if UserDefaults.standard.getChatStyle() == .Snapchat 
                return true
            
            return message.sender.id == "user1"
        
    

转到 MessagesCollectionViewFlowLayout


fileprivate extension MessagesCollectionViewFlowLayout 

    func avatarPosition(for attributes: MessageIntermediateLayoutAttributes) -> AvatarPosition 
        var position = messagesLayoutDelegate.avatarPosition(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView)

        switch position.horizontal 
        case .cellTrailing, .cellLeading:
            break
        case .natural:
            position.horizontal = messagesDataSource.**isFromUser1Custom**(message: attributes.message) ? .cellLeading : .cellTrailing
        

        return position
    

你的 ChatViewController

extension ChatViewController: MessagesDisplayDelegate 

    func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle 
//do what you want to
        let corner: MessageStyle.TailCorner = isFromUser1Custom ? .bottomRight : .bottomLeft
            return  MessageStyle.bubbleTail(corner, .curved)
    





extension ChatViewController: MessagesLayoutDelegate 

    func messagePadding(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets 
        if isFromUser1Custom(message: message) 
            return UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 30)
        
        else 
            return UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 4)
        
    

    func cellTopLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment 
        if isFromUser1Custom(message: message) 
            return .messageLeading(UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0))
        
        else 
            return .messageTrailing(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10))
        
    

    func cellBottomLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment 
        if isFromUser1Custom(message: message) 
            return .messageLeading(UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0))
         else 
            return .messageTrailing(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10))
        
    

    func cellTopupLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment 

        if isFromUser1Custom(message: message) 
            return .messageLeading(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        
        else 
            return .messageTrailing(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        
    

【讨论】:

以上是关于消息线程`MessageKit`swift的左对齐的主要内容,如果未能解决你的问题,请参考以下文章

在MessageKit中轻按以放大图像视图-Swift

官方示例中消息左对齐和右对齐的条件在哪里?

MessageKit InputBar 在 ViewController 关闭时被隐藏/删除

委托和数据源在 MessageKit Swift IOS 中不起作用

我可以使用 messageKIt 在 swift 5 ios 中创建像这样的自定义单元格吗?

MessageKit:添加的输入栏按钮仅在我发送消息后出现