Quickblox communication backendを使用するiOSチャットアプリケーション用のエレガントなレディーチャットビューコントローラー。
#特徴
Quickbloxに簡単に接続できます。
pod 'QMChatViewController'
QMChatViewControllerフォルダーをプロジェクトフォルダーにドラッグし、適切なターゲットにリンクします。
依存関係をインストールします。
例はリポジトリに含まれています。チャットビューコントローラーの仕組みを確認するために試してみてください。
QMChatViewControllerをアプリに追加する手順:
QMChatViewControllerのサブクラスを作成します。コードとインターフェイスビルダーの両方から作成できます。
QMChatViewControllerのサブクラスを開き、 ViewDidLoadメソッドで以下を実行します。
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
}チャットメッセージに固有のセルビュークラスを返す:
- ( Class )viewClassForItem:(QBChatMessage *)item {
// Cell class for message
if (item. senderID != self. senderID ) {
return [QMChatIncomingCell class ];
}
else {
return [QMChatOutgoingCell class ];
}
return nil ;
}
セルのサイズと最小幅を計算します。
- ( 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;
}制約を変更せずにコレクションチャットセルの属性を変更する:
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は発信添付ファイルに使用されます。どちらも、読み込みの進行状況を表示するためのProgressラベルがあります。 Xibも含まれています。
QMChatViewControllerには、 QMChatDataSourceと呼ばれるデータソースマネージャーが含まれています。 QMChatViewControllerで作業する必要があるすべての方法が実装されています。このクラスは、データソースからメッセージを追加、更新、削除するために使用する必要があります。 QMChatDataSourceには代表者がいます。これは、データソースが変更されたときはいつでも呼び出されます。
メソッドとその使用の詳細については、 QMChatDataSource.hのインラインドキュメントをご覧ください。
インラインコードドキュメントが利用可能。
ライセンスを参照してください