在消息包聊天中显示用户名?
Display username in messagekit chat?
我正在努力寻找一种在 MessageKit 聊天视图控制器中显示用户名的方法。我从示例项目中复制了 MessagesDataSource,但没有成功。这是我的 MessagesDataSource 当前的样子:
extension ChatViewController: MessagesDataSource {
func currentSender() -> Sender {
//guard let currentUserID = User.current?.key else {return nil}
let newSender = Sender(id: (User.current?.key)!, displayName: (User.current?.username)!)
return newSender
}
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
//return 1
return messages.count
}
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
return messages[indexPath.section]
}
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.darkGray])
}
func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let name = message.sender.displayName
return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
}
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let dateString = formatter.string(from: message.sentDate)
return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
}
}
如果我在 let name = message.sender.displayName
之后放置一个断点,正确的用户名将显示在控制台输出中。但是,它仍然没有出现在聊天记录中。聊天视图仍然只显示头像和消息,没有其他内容。
我错过了什么?
谢谢
虽然我不知道是什么,您使用的 MessageKit
版本,但我很确定如果您尝试了这个,一切都会正常,
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let name = message.sender.displayName
return NSAttributedString(
string: name,
attributes: [
.font: UIFont.preferredFont(forTextStyle: .caption1),
.foregroundColor: UIColor(white: 0.3, alpha: 1)
]
)
}
因为我没有看到任何名为 messageTopLabelAttributedText
的 dataSource
函数
请在下面添加此代码:
func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
return 35
}
我正在努力寻找一种在 MessageKit 聊天视图控制器中显示用户名的方法。我从示例项目中复制了 MessagesDataSource,但没有成功。这是我的 MessagesDataSource 当前的样子:
extension ChatViewController: MessagesDataSource {
func currentSender() -> Sender {
//guard let currentUserID = User.current?.key else {return nil}
let newSender = Sender(id: (User.current?.key)!, displayName: (User.current?.username)!)
return newSender
}
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
//return 1
return messages.count
}
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
return messages[indexPath.section]
}
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.darkGray])
}
func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let name = message.sender.displayName
return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
}
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let dateString = formatter.string(from: message.sentDate)
return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
}
}
如果我在 let name = message.sender.displayName
之后放置一个断点,正确的用户名将显示在控制台输出中。但是,它仍然没有出现在聊天记录中。聊天视图仍然只显示头像和消息,没有其他内容。
我错过了什么?
谢谢
虽然我不知道是什么,您使用的 MessageKit
版本,但我很确定如果您尝试了这个,一切都会正常,
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let name = message.sender.displayName
return NSAttributedString(
string: name,
attributes: [
.font: UIFont.preferredFont(forTextStyle: .caption1),
.foregroundColor: UIColor(white: 0.3, alpha: 1)
]
)
}
因为我没有看到任何名为 messageTopLabelAttributedText
dataSource
函数
请在下面添加此代码:
func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
return 35
}