我从聊天中删除了头像,我可以移动消息本身,但我无法移动消息的日期。怎么做?
还有一个问题:我在每四条消息之后写一个消息日期。如何只在第二天开始时才写时间?
屏幕上是发生的事情的一个例子。
class ChatsViewController: MessagesViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
layout.textMessageSizeCalculator.outgoingAvatarSize = .zero
layout.textMessageSizeCalculator.incomingAvatarSize = .zero
layout.photoMessageSizeCalculator.outgoingAvatarSize = .zero
layout.photoMessageSizeCalculator.incomingAvatarSize = .zero
layout.attributedTextMessageSizeCalculator.incomingAvatarSize = .zero
layout.attributedTextMessageSizeCalculator.outgoingAvatarSize = .zero
layout.attributedTextMessageSizeCalculator.avatarLeadingTrailingPadding = .zero
}
}
}
// MARK: - MessagesDataSource
extension ChatsViewController: MessagesDataSource {
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
if indexPath.item % 4 == 0 {
return NSAttributedString(
string: MessageKitDateFormatter.shared.string(from: message.sentDate),
attributes: [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.1176470588, green: 0.4470588235, blue: 0.8, alpha: 1)])
} else {
return nil
}
}
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
}
}
// MARK: - MessagesLayoutDelegate
extension ChatsViewController: MessagesLayoutDelegate {
func footerViewSize(for section: Int, in messagesCollectionView: MessagesCollectionView) -> CGSize {
return CGSize(width: 0, height: 8)
}
func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
if (indexPath.item) % 4 == 0 {
return 30
} else {
return 0
}
}
func messageBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
return 16
}
}
// MARK: - MessagesDisplayDelegate
extension ChatsViewController: MessagesDisplayDelegate {
func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
avatarView.isHidden = true
}
func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
return .zero
}
func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
return .bubble
}
}