JSQMessagesViewController 消息气泡对齐不规则

JSQMessagesViewController message bubbles are aligning irregular

我正在使用 JSQMessagesViewController 开发应用程序。但是在 collectionView 中存在消息气泡排列不规则的问题。

这是截图

代码如下:

import UIKit

class mDetailContainerViewController: JSQMessagesViewController, JSQMessagesCollectionViewDelegateFlowLayout{


    var userName = ""
    var messages = [JSQMessage]()
    let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
    let outgoingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.greenColor())

    override func viewDidLoad() {
        super.viewDidLoad()

        self.userName = "iPhone"

        for i in 1...10{

            var sender =  (i%2 == 0) ? "Syncano" : self.userName
            var message = JSQMessage(senderId: sender, displayName: sender, text: "Text")
            self.messages += [message]


        }

        self.collectionView.reloadData()
        self.senderDisplayName = self.userName
        self.senderId = self.userName
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
        var data = self.messages[indexPath.row]
        return data
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
        var data = self.messages[indexPath.row]
        if (data.senderId == self.senderId){

            return self.outgoingBubble

        }else{

            return self.incomingBubble

        }
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
        return nil
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.messages.count
    }

    override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
        var message = JSQMessage(senderId: senderId, displayName: senderDisplayName, text: text)
        messages += [message]
        self.finishSendingMessage()
    }

    override func didPressAccessoryButton(sender: UIButton!) {

    }

我在网上搜索了一下解决方案,但是所有的说明都很复杂。我什么也没弄清楚。此外,我不确定我是否找到了解决方案。如果有人简单地解释解决方案,我会很高兴。

注意:JSQMessagesViewController显示在容器视图中。

最后,如何使用本地化更改发送按钮标题和文本字段占位符。

谢谢。

我不熟悉这个SDK,但我认为这是因为这两行

let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
let outgoingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.greenColor())

incomingMessagesBubbleImageWithColor。将 outgoingBubble 设置为 JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(如果存在)

而且我之前从未使用过此消息 class,但我相信您与绿色文本的对齐问题可能会通过类似...

的方式解决
outgoingBubble.textLabel.textAlignment = NSTextAlignment.Center

要修复气泡位置,请使用此代码删除头像图片;

self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero
self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero

对于将来遇到此问题的任何人,请检查您的代码是否如下所示:

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {

    let data = messages[indexPath.row]

    if data.senderId == currentUser.objectId! {
       return incomingBubble
    } else {
        return outgoingBubble
    }
}

它应该是这样的:

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {

    let data = messages[indexPath.row]

    if data.senderId == PFUser.currentUser()!.objectId! {
        return outgoingBubble
    } else {
        return incomingBubble
    }
}

帮我修好了!

Swift 3:

对于将气泡内的文本居中的特定问题:

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

    cell.textView.textAlignment = .center

    return cell
}