如何从messageKit中的头像中删除缩进?
how to remove indentation from avatar in messageKit?
我从聊天中删除了头像,我可以移动消息本身,但我无法移动消息的日期。怎么做?
还有一个问题:我每四条消息后写一个消息日期。如何让时间只在第二天开始时写入?
屏幕上是所发生情况的示例。
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
}
}
您可以在流式布局上使用 setMessageIncomingMessageBottomLabelAlignment
和 setMessageOutgoingMessageBottomLabelAlignment
方法来调整底部标签对齐方式,如下所示:
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageIncomingMessageBottomLabelAlignment(LabelAlignment(textAlignment: .left, textInsets: .zero))
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageOutgoingMessageBottomLabelAlignment(LabelAlignment(textAlignment: .right, textInsets: .zero))
要回答您的第二个问题,您可以在 class var lastDisplayedSentDate: Date?
和 cellTopLabelAttributedText
方法中保留一个变量,检查 lastDisplayedSentDate 是否与当前消息,return nil
如果它们在同一天,否则 return 日期字符串并更新 lastDisplayedSentDate
变量:
if let lastDisplayedSentDate = lastDisplayedSentDate, Calendar.current.isDate(lastDisplayedSentDate, inSameDayAs: message.sentDate) {
return nil
}
lastDisplayedSentDate = message.sentDate
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)])
您可以在 cellTopLabelHeight
方法中添加类似的逻辑。
我从聊天中删除了头像,我可以移动消息本身,但我无法移动消息的日期。怎么做?
还有一个问题:我每四条消息后写一个消息日期。如何让时间只在第二天开始时写入?
屏幕上是所发生情况的示例。
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
}
}
您可以在流式布局上使用 setMessageIncomingMessageBottomLabelAlignment
和 setMessageOutgoingMessageBottomLabelAlignment
方法来调整底部标签对齐方式,如下所示:
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageIncomingMessageBottomLabelAlignment(LabelAlignment(textAlignment: .left, textInsets: .zero))
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageOutgoingMessageBottomLabelAlignment(LabelAlignment(textAlignment: .right, textInsets: .zero))
要回答您的第二个问题,您可以在 class var lastDisplayedSentDate: Date?
和 cellTopLabelAttributedText
方法中保留一个变量,检查 lastDisplayedSentDate 是否与当前消息,return nil
如果它们在同一天,否则 return 日期字符串并更新 lastDisplayedSentDate
变量:
if let lastDisplayedSentDate = lastDisplayedSentDate, Calendar.current.isDate(lastDisplayedSentDate, inSameDayAs: message.sentDate) {
return nil
}
lastDisplayedSentDate = message.sentDate
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)])
您可以在 cellTopLabelHeight
方法中添加类似的逻辑。