Messagekit: Is there a way to create a custom design view with buttons, image and text together in message listing (like LocationMessageCell, MediaMessageCell etc) ?

Created on 17 Feb 2018  路  4Comments  路  Source: MessageKit/MessageKit

General Information

  • MessageKit Version: 0.13.1

  • iOS Version(s): All

  • Swift Version: Swift4

  • Devices/Simulators: All

  • Reproducible in ChatExample? (Yes/No): Yes

What happened?

Unable to create a custom chat bubble design view with texts, images and buttons.

What did you expect to happen?

I am looking to add a custom chat bubble design view which mat contain texts, images and button

question

Most helpful comment

@anandatswayam A few things you'll need to do (this is the path of least resistance):

  • Subclass MessageCollectionViewCell (or one of its existing subclasses) and implement the configure method.
  • Override cellForItem(...) (make sure to call cell.configure here
  • Override sizeForItem(...)
  • For the style of the cell itself, this is what I do:
func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
    let closure = { (view: MessageContainerView) in
       view.layer.cornerRadius = 15.0
       view.layer.borderWidth = 1.0
       view.layer.borderColor = UIColor.gray.cgColor
       view.layer.backgroundColor = UIColor.white.cgColor
    }
    return .custom(closure)
}

Hopefully I'm not missing anything!

All 4 comments

@anandatswayam A few things you'll need to do (this is the path of least resistance):

  • Subclass MessageCollectionViewCell (or one of its existing subclasses) and implement the configure method.
  • Override cellForItem(...) (make sure to call cell.configure here
  • Override sizeForItem(...)
  • For the style of the cell itself, this is what I do:
func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
    let closure = { (view: MessageContainerView) in
       view.layer.cornerRadius = 15.0
       view.layer.borderWidth = 1.0
       view.layer.borderColor = UIColor.gray.cgColor
       view.layer.backgroundColor = UIColor.white.cgColor
    }
    return .custom(closure)
}

Hopefully I'm not missing anything!

You have two options for this in the latest version of MessageKit. You can either:

  1. Subclass MessageCollectionViewCell which is a pure wrapper on UICollectionViewCell
  2. Subclass MessageContentCell which has the AvatarView, messageTopLabel, messageBottomLabel, cellTopLabel, and an empty MessageContainerView

You would be responsible for returning a CellSizeCalculator to size any of these new cells in the layout object:

https://github.com/MessageKit/MessageKit/blob/69c5414c224a98891e14984e83228740e4339219/Sources/Layout/MessagesCollectionViewFlowLayout.swift#L134

If you need to create a different "kind" of message, ie) passing different message content, you can use the MessageKind.custom(Any?) case

@benhas999 please do not create a new issue #844 while also adding the same query to another thread.

Sent with GitHawk

@SD10

I've find some exemples with MessageCollectionViewCell, but it didn't find any examples with MessageContentCell did you know where I can find some ?

I've try to implement MessageContentCell in the same way as MessageCollectionViewCell, but I didn't succeed because I use a XIB

It is possible to implement it on a XIB ?

Thanks for your help guys

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yspreen picture yspreen  路  3Comments

TheDarkCode picture TheDarkCode  路  3Comments

ChandraPrakashJangid picture ChandraPrakashJangid  路  3Comments

ahmedwasil picture ahmedwasil  路  3Comments

NiklasWilson picture NiklasWilson  路  4Comments