Um elegante controlador de visualização de bate-papo pronto para uso para aplicativos de bate-papo iOS que usam Quickblox communication backend .
#Características
Quickblox .
pod 'QMChatViewController'
Arraste a pasta QMChatViewController para a pasta do projeto e vincule o destino apropriado.
Instalar dependências.
Um exemplo está incluído no repositório. Experimente para ver como funciona o controlador de exibição de bate -papo.
Etapas para adicionar QMChatViewController ao seu aplicativo:
Crie uma subclasse do QMChatViewController . Você pode criá -lo a partir do código de código e interface.
Abra sua subclasse do QMChatViewController e faça o seguinte no método ViewDidload :
self.senderID = 2000 ;
self.senderDisplayName = @" user1 " ;[ self .chatDataSource addMessages: <array of messages>];Lidar com o envio de mensagens.
- ( 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
}Retornar as classes de exibição de células específicas para a mensagem de bate -papo:
- ( Class )viewClassForItem:(QBChatMessage *)item {
// Cell class for message
if (item. senderID != self. senderID ) {
return [QMChatIncomingCell class ];
}
else {
return [QMChatOutgoingCell class ];
}
return nil ;
}
Calcule o tamanho da célula e a largura mínima:
- ( 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 ;
}Etiquetas superior, inferior e de texto.
- ( 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;
}Modificando os atributos da célula de bate -papo de coleção sem alterar as restrições:
struct QMChatLayoutModel {
CGSize avatarSize;
CGSize containerSize;
UIEdgeInsets containerInsets;
CGFloat topLabelHeight;
CGFloat bottomLabelHeight;
CGSize staticContainerSize;
CGFloat maxWidthMarginSpace;
};
typedef struct QMChatLayoutModel QMChatCellLayoutModel;Você pode modificar esses atributos neste método:
- (QMChatCellLayoutModel)collectionView:(QMChatCollectionView *)collectionView layoutModelAtIndexPath:( NSIndexPath *)indexPath {
QMChatCellLayoutModel layoutModel = [ super collectionView: collectionView layoutModelAtIndexPath: indexPath];
// Update attributes here ...
return layoutModel;
}Portanto, se você deseja ocultar a etiqueta superior ou a etiqueta inferior, basta definir a altura deles como 0.
QMChatViewController suporta mensagens de célula de anexo de imagem. QMChatAttachmentIncomingCell é usado para anexos recebidos, QMChatAttachmentOutgoingCell é usado para anexos de saída. Ambos têm o rótulo de progresso para exibir o progresso do carregamento. Os XIB também estão incluídos.
QMChatViewController contém seu gerente de fonte de dados chamado QMChatDataSource . Possui implementação de todos os métodos, que você precisa trabalhar com QMChatViewController . Esta classe deve ser usada para adicionar, atualizar e excluir mensagens da fonte de dados. QMChatDataSource tem delegado, que chamou sempre que a fonte de dados era modificada.
Para obter mais informações sobre métodos e seu uso, consulte nosso documento em linha no QMChatDataSource.h .
Documentação do código embutido disponível.
Consulte a licença