Ios JSQMessage的自定义单元格
我正在尝试创建一个自定义消息单元格,该单元格有一个图标(在右侧用于传入,在左侧用于传出) 我已经对JSQMessageCollectionViewCeloutGong进行了子类化,并将图标显示在我想要的位置。伟大的我没能做到的是将文本视图的宽度缩小,以考虑图标。我原以为这是自定义传出xib文件中的一个简单的自动布局更改,但当应用程序运行时,自动布局宽度被覆盖Ios JSQMessage的自定义单元格,ios,jsqmessagesviewcontroller,Ios,Jsqmessagesviewcontroller,我正在尝试创建一个自定义消息单元格,该单元格有一个图标(在右侧用于传入,在左侧用于传出) 我已经对JSQMessageCollectionViewCeloutGong进行了子类化,并将图标显示在我想要的位置。伟大的我没能做到的是将文本视图的宽度缩小,以考虑图标。我原以为这是自定义传出xib文件中的一个简单的自动布局更改,但当应用程序运行时,自动布局宽度被覆盖 如何(直接或间接地)从jsqMessageCollectionViewCell或jsqMessageCollectionViewCell的
如何(直接或间接地)从
jsqMessageCollectionViewCell
或jsqMessageCollectionViewCell的子类更新textViewMarginHorizontalSpaceConstraint
中的jsqMessageCollectionViewCell
我建议这样做
extension JSQMessagesCollectionViewCellOutgoing {
public func messageContentSmaller() {
self.messageBubbleContainerView?.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: .Horizontal)
}
}
您可能还需要
self.messageBubbleContainerView.setNeedsLayout
如果有帮助,请告诉我。您不必创建JSQMessagesCollectionViewCell的任何扩展
除非你想改变小区的结构,比如改变发送者/接收者化身的位置或上/下标签的位置
我对JSQMessage非常陌生,但我的解决方案如下:
如果您只需要消息主要内容部分的自定义视图,即冒泡圆角部分:),则
您只需要一个自定义的JSQMediaItem类
然后你就覆盖了
- (UIView *)mediaView
方法,则可以完全控制内容视图
诀窍在于,您必须先获得内容视图的高度
- (CGSize)mediaViewDisplaySize
这个方法由JSQ框架调用。
因此,最好的方法是在自定义类的init方法中动态计算返回视图的高度:
- (instancetype)initWithImage:(UIImage *)image title:(NSString*)title message:(NSString*)msg
这是我的自定义类,需要显示事件消息、标题、图像和消息。顺便说一句,只有消息部分高度是动态的,所以我只是使用
[NSString boundingRectWithSize:]
EventMediaItem.h
#import <JSQMessagesViewController/JSQMediaItem.h>
NS_ASSUME_NONNULL_BEGIN
/**
* The `EventMediaItem` class is a concrete `JSQMediaItem` subclass that implements the `JSQMessageMediaData` protocol
* and represents a event media message. An initialized `EvnetMediaItem` object can be passed
* to a `JSQMediaMessage` object during its initialization to construct a valid media message object.
* You may wish to subclass `EventMediaItem` to provide additional functionality or behavior.
*/
@interface EventMediaItem : JSQMediaItem <JSQMessageMediaData, NSCoding, NSCopying>
/**
* The image for the event media item. The default value is `nil`.
*/
@property (copy, nonatomic, nullable) UIImage *image;
/**
* The title for the event media item. The default value is `nil`.
*/
@property (copy, nonatomic, nullable) NSString *title;
/**
* The message for the event media item. The default value is `nil`.
*/
@property (copy, nonatomic, nullable) NSString *message;
/**
* Initializes and returns a event media item object having the given image.
*
* @param image The image for the event media item. This value may be `nil`.
* @param title The title for the event media item. This value may be `nil`.
* @param message The message for the event media item. This value may be `nil`.
*
* @return An initialized `EventMediaItem`.
*
* @discussion If the image must be dowloaded from the network,
* you may initialize a `EventMediaItem` object with a `nil` image.
* Once the image has been retrieved, you can then set the image property.
*/
- (instancetype)initWithImage:(UIImage *)image title:(NSString*)title message:(NSString*)msg;
@end
NS_ASSUME_NONNULL_END
VobMessageMediaCellIncoming只是您当前使用的任何输入消息单元格的副本,根据我的经验,您必须复制一个具有不同标识符的新单元格,而不是对正常消息和媒体消息使用相同的nib,否则在重用单元格时,JSQMessageCollectionView可能会混淆并显示异常消息
然后,在cellForItemAtIndexPath中,您可以将逻辑分叉为而不是设置单元格的文本视图,因为相应的消息是媒体类型(EventMediaItem),并且单元格的任何文本视图设置都会导致断言失败:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
let message = viewModel.messageBubbleModelView(indexPath.item)
if !message.isMediaMessage {
let color = senderId == message.senderId ? UIColor.whiteColor() : AppColor.BodyTextColor
cell.textView.textColor = color
cell.textView.linkTextAttributes = [
NSForegroundColorAttributeName: color,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
]
}
//set position nickname
cell.messageBubbleTopLabel.textAlignment = .Left
let edgeInset = cell.messageBubbleTopLabel.textInsets
cell.messageBubbleTopLabel.textInsets = UIEdgeInsetsMake(edgeInset.top, edgeInset.left - 20, edgeInset.bottom, edgeInset.right + 20)
...
}
顺便说一句,isMediaMessage属性来自JSQMessage,如果您初始化JSQMessage并将其设置为true;
最后,在自定义JSQMessage中:
(MessagePost是我自己项目的消息模型)
似乎还有很多其他方法可以实现这一点,您可以参考以下链接:
但是,我无法从这些线程中获得任何有用的信息--# 请说明你是如何解决的
self.collectionView.registerNib(UINib(nibName: "VobMessagesMediaCellIncoming", bundle: nil), forCellWithReuseIdentifier: "VobMessagesMediaCellIncoming")
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
let message = viewModel.messageBubbleModelView(indexPath.item)
if !message.isMediaMessage {
let color = senderId == message.senderId ? UIColor.whiteColor() : AppColor.BodyTextColor
cell.textView.textColor = color
cell.textView.linkTextAttributes = [
NSForegroundColorAttributeName: color,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
]
}
//set position nickname
cell.messageBubbleTopLabel.textAlignment = .Left
let edgeInset = cell.messageBubbleTopLabel.textInsets
cell.messageBubbleTopLabel.textInsets = UIEdgeInsetsMake(edgeInset.top, edgeInset.left - 20, edgeInset.bottom, edgeInset.right + 20)
...
}
class MessageBubbleViewModel: JSQMessage {
let messagePost: MessagePost
init(messagePost: MessagePost) {
self.messagePost = messagePost
if messagePost.isEvent {
let eventMedia:EventMediaItem = EventMediaItem(image: UIImage(named: "some cool image")!, title: messagePost.title, message: messagePost.msg)
super.init(senderId: String(messagePost.senderId),
senderDisplayName: messagePost.senderNickName,
date: messagePost.updatedAt,
media: eventMedia)
}else{
super.init(
senderId: String(messagePost.senderId),
senderDisplayName: messagePost.senderNickName,
date: messagePost.updatedAt,
text: messagePost.text)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}