QMChatViewController
为使用 Quickblox 通信后端的 iOS
聊天应用提供了一个优雅的、现成的聊天视图控制器。
#特性
- 带有一组单元的现成的聊天视图控制器。
- 自动计算单元大小。
- 可自定义的聊天单元 UI。
- 易于改进和扩展功能。
- 易于与
Quickblox
连接。 - 优化且性能出色。
- 支持竖直和横屏方向。
- 内部自动布局。
屏幕截图
要求
- iOS 8.0+
- ARC
- Xcode 6+
- Quickblox SDK 2.0+
- TTTAttributedLabel
- SDWebImage
安装
CocoaPods
pod 'QMChatViewController'
手动
-
将
QMChatViewController
文件夹拖到您的项目文件夹中,并将其链接到相应的目标。 -
安装依赖项。
依赖
开始
仓库中包含一个示例。尝试一下,看看聊天视图控制器是如何工作的。
将QMChatViewController
添加到您的应用的步骤
-
创建
QMChatViewController
的子类。您可以从代码和Interface Builder中创建它。 -
打开您的
QMChatViewController
子类,并在viewDidLoad方法中执行以下操作- 配置聊天发送者ID和显示名称
self.senderID = 2000; self.senderDisplayName = @"user1";
- 使用相应的方法插入消息
[self.chatDataSource addMessages:<array of messages>];
-
处理消息发送
- (void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSUInteger)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date { // Add sending message - for example: QBChatMessage *message = [QBChatMessage message]; message.text = text; message.senderID = senderId; QBChatAttachment *attacment = [[QBChatAttachment alloc] init]; message.attachments = @[attacment]; [self.chatDataSource addMessage:message]; [self finishSendingMessageAnimated:YES]; // Save message to your cache/memory storage. // Send message using Quickblox SDK }
-
返回聊天消息特定的cell视图类
- (Class)viewClassForItem:(QBChatMessage *)item { // Cell class for message if (item.senderID != self.senderID) { return [QMChatIncomingCell class]; } else { return [QMChatOutgoingCell class]; } return nil; }
-
计算cell的大小和最小宽度
- (CGFloat)collectionView:(QMChatCollectionView *)collectionView minWidthAtIndexPath:(NSIndexPath *)indexPath { QBChatMessage *item = [self.chatDataSource messageForIndexPath:indexPath]; NSAttributedString *attributedString = item.senderID == self.senderID ? [self bottomLabelAttributedStringForItem:item] : [self topLabelAttributedStringForItem:item]; CGSize size = [TTTAttributedLabel sizeThatFitsAttributedString:attributedString withConstraints:CGSizeMake(1000, 10000) limitedToNumberOfLines:1]; return size.width; }
-
顶部、底部和文本标签。
- (NSAttributedString *)attributedStringForItem:(QBChatMessage *)messageItem { UIColor *textColor = [messageItem senderID] == self.senderID ? [UIColor whiteColor] : [UIColor colorWithWhite:0.290 alpha:1.000]; UIFont *font = [UIFont fontWithName:@"Helvetica" size:15]; NSDictionary *attributes = @{NSForegroundColorAttributeName:textColor, NSFontAttributeName:font}; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:messageItem.text attributes:attributes]; return attrStr; } - (NSAttributedString *)topLabelAttributedStringForItem:(QBChatMessage *)messageItem { if (messageItem.senderID == self.senderID) { return nil; } UIFont *font = [UIFont fontWithName:@"Helvetica" size:14]; UIColor *textColor = [UIColor colorWithRed:0.184 green:0.467 blue:0.733 alpha:1.000]; NSDictionary *attributes = @{NSForegroundColorAttributeName:textColor, NSFontAttributeName:font}; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"nickname" attributes:attributes]; return attrStr; } - (NSAttributedString *)bottomLabelAttributedStringForItem:(QBChatMessage *)messageItem { UIFont *font = [UIFont fontWithName:@"Helvetica" size:12]; UIColor *textColor = [messageItem senderID] == self.senderID ? [UIColor colorWithWhite:1.000 alpha:0.510] : [UIColor colorWithWhite:0.000 alpha:0.490]; NSDictionary *attributes = @{NSForegroundColorAttributeName:textColor, NSFontAttributeName:font}; NSString *dateStr = @"10:20"; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:dateStr attributes:attributes]; return attrStr; }
-
在不更改约束的情况下修改集合聊天cell属性
struct QMChatLayoutModel { CGSize avatarSize; CGSize containerSize; UIEdgeInsets containerInsets; CGFloat topLabelHeight; CGFloat bottomLabelHeight; CGSize staticContainerSize; CGFloat maxWidthMarginSpace; }; typedef struct QMChatLayoutModel QMChatCellLayoutModel;
- 头像图片视图的大小
- 消息视图容器的大小
- 顶部标签的高度
- 底部标签的高度
- 容器视图的静态大小
- 消息和屏幕末端之间的间距
您可以在该方法中修改这些属性
- (QMChatCellLayoutModel)collectionView:(QMChatCollectionView *)collectionView layoutModelAtIndexPath:(NSIndexPath *)indexPath { QMChatCellLayoutModel layoutModel = [super collectionView:collectionView layoutModelAtIndexPath:indexPath]; //Update attributes here ... return layoutModel; }
因此,如果您想隐藏顶部标签或底部标签,只需将它们的高度设置为0即可。
附件
QMChatViewController
支持图片附件细胞消息。使用QMChatAttachmentIncomingCell
来接收附件,使用QMChatAttachmentOutgoingCell
来发送附件。它们都有进度标签来显示加载进度。XIB也已经包含在内。
聊天数据源
QMChatViewController
包含其数据源管理器QMChatDataSource
。它实现了所有需要与QMChatViewController
一起工作的方法。应使用此类来添加、更新和从数据源删除消息。QMChatDataSource
具有事件代理,每当数据源被修改时都会调用。
有关方法和其使用方法,请查阅QMChatDataSource.h
内的调用电文说明。
问题与帮助
- 如果您在体验任何问题,您可以在GitHub上创建一个问题。我们将很高兴为您提供帮助。
- 或者,您可以在StackOverflow上提出一个标记为'quickblox'的问题http://stackoverflow.com/questions/ask
文档
提供内联代码文档。