如何使用 JSQMessageViewController 使发件人显示名称出现?
How do you make the sender display name appear with JSQMessageViewController?
我有以下函数被调用以添加消息:
func addMessage(text: String, displayName: String) {
let message = JSQMessage(senderId: "tester", displayName: displayName, text: text)
messages.append(message)
finishReceivingMessage()
}
然后在这个函数中
override func collectionView(collectionView: JSQMessagesCollectionView!,
messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
I return 该 indexPath 的消息日期。该消息正确显示,但没有显示名称。
我认为你错过了 attributedTextForMessageBubbleTopLabelAtIndexPath
应该看起来像这样
override func collectionView(collectionView: JSQMessagesCollectionView?, attributedTextForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> NSAttributedString! {
let message = messages[indexPath.item]
switch message.senderId {
case CURRENTUSERID:
return nil
default:
guard let senderDisplayName = message.senderDisplayName else {
assertionFailure()
return nil
}
return NSAttributedString(string: senderDisplayName)
}
}
编辑:
还要确保使用此功能为标签赋予高度
override func collectionView(collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 13 //or what ever height you want to give
}
祝你好运
确保添加此功能以显示姓名:
override func collectionView(collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 15
}
新的更新方法
override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAt indexPath: IndexPath!) -> NSAttributedString!
{
let message = messages[indexPath.item]
if message.senderId == senderId {
return nil
} else {
guard let senderDisplayName = message.senderDisplayName else {
assertionFailure()
return nil
}
return NSAttributedString(string: senderDisplayName)
}
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAt indexPath: IndexPath!) -> CGFloat
{
//return 17.0
let message = messages[indexPath.item]
if message.senderId == senderId {
return 0.0
} else {
return 17.0
}
}
这是 swift 3
的代码
override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAt indexPath: IndexPath!) -> NSAttributedString! {
return NSAttributedString(string: senderDisplayName)
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAt indexPath: IndexPath!) -> CGFloat {
return 15 //your height
}
我有以下函数被调用以添加消息:
func addMessage(text: String, displayName: String) {
let message = JSQMessage(senderId: "tester", displayName: displayName, text: text)
messages.append(message)
finishReceivingMessage()
}
然后在这个函数中
override func collectionView(collectionView: JSQMessagesCollectionView!,
messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
I return 该 indexPath 的消息日期。该消息正确显示,但没有显示名称。
我认为你错过了 attributedTextForMessageBubbleTopLabelAtIndexPath
应该看起来像这样
override func collectionView(collectionView: JSQMessagesCollectionView?, attributedTextForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> NSAttributedString! {
let message = messages[indexPath.item]
switch message.senderId {
case CURRENTUSERID:
return nil
default:
guard let senderDisplayName = message.senderDisplayName else {
assertionFailure()
return nil
}
return NSAttributedString(string: senderDisplayName)
}
}
编辑:
还要确保使用此功能为标签赋予高度
override func collectionView(collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 13 //or what ever height you want to give
}
祝你好运
确保添加此功能以显示姓名:
override func collectionView(collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 15
}
新的更新方法
override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAt indexPath: IndexPath!) -> NSAttributedString!
{
let message = messages[indexPath.item]
if message.senderId == senderId {
return nil
} else {
guard let senderDisplayName = message.senderDisplayName else {
assertionFailure()
return nil
}
return NSAttributedString(string: senderDisplayName)
}
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAt indexPath: IndexPath!) -> CGFloat
{
//return 17.0
let message = messages[indexPath.item]
if message.senderId == senderId {
return 0.0
} else {
return 17.0
}
}
这是 swift 3
的代码override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAt indexPath: IndexPath!) -> NSAttributedString! {
return NSAttributedString(string: senderDisplayName)
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAt indexPath: IndexPath!) -> CGFloat {
return 15 //your height
}