MXRMessenger 0.2.1

MXRMessenger 0.2.1

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布时间最新版本2018年1月

Scott Kensell 维护。



  • Scott Kensell

MXRMessenger

Version
License
Platform

为什么需要另一个聊天库?

MXRMessenger 是一个可定制的聊天库,旨在提供流畅滚动和响应式体验。我之所以要编写这个库,是因为:

话虽如此,如果您从未使用过 Texture,您可能最好选择上述更成熟的库之一。这个库也几乎未经测试,到目前为止仅在1个生产应用程序中使用,所以请自行承担风险。

功能

Screenshot 1 Screenshot 2

MXRMessengerViewController 类似于 SlackTextViewController 的简化版,可以单独通过 pod 'MXRMessenger/ViewController' 包括。它具有:

  • 一个可以交互式隐藏的输入工具栏。
  • 一个具有最大行数的可增长输入文本节点。
  • 可自定义的字体、颜色和按钮。

其他 subspec pod 'MXRMessenger/MessageCell' 提供带有友好工厂的 Facebook 风格气泡。主要特点:

  • 发送文本、图片、视频,或同时发送多个图片和/或视频。
  • 自动更新边缘对齐,以将来自同一发送者的消息分组。
  • 自动日期格式化。
  • 每次发送单个图片时,图片会动态更新其大小以匹配实际的长宽比。
  • 复制、删除和简单的选项菜单项。
  • 点击/手势回调。
  • 完全可定制的,无论是从左到右、没有头像、间距问题、字体或颜色问题,有很多都是可定制的。

并且因为我们依赖于 Texture,您还可以免费获得 60 fps 的流畅度。

安装

MXRMessenger 可以通过 CocoaPods 获取。

对于完整的聊天库,请在 Podfile 中添加 pod 'MXRMessenger'

如果只需要 ViewController,请添加 pod 'MXRMessenger/ViewController'

如果只需要 Facebook 风格的气泡,请添加 pod 'MXRMessenger/MessageCell'

需要在必要时添加

#import <MXRMessenger/MXRMessenger.h>

使用

继承 MXRMessengerViewController 子类。然后,使用提供的 MXRMessageCellFactory 是实例化信息单元格的最简单方法。因此创建一个强属性

@property (nonatomic, strong) MXRMessageCellFactory* cellFactory;

大多数自定义操作在初始化时发生。您可以复制粘贴以下代码,仅删除或自定义您想要的任何内容。然后实现编译器关于代理或数据源方法的抱怨。

- (instancetype)init {
    MXRMessengerInputToolbar* toolbar = [[MXRMessengerInputToolbar alloc] initWithFont:[UIFont systemFontOfSize:16.0f] placeholder:@"Type a message" tintColor:[UIColor mxr_fbMessengerBlue]];
    self = [super initWithToolbar:toolbar];
    if (self) {
        // add extra buttons to toolbar
        MXRMessengerIconButtonNode* addPhotosBarButtonButtonNode = [MXRMessengerIconButtonNode buttonWithIcon:[[MXRMessengerPlusIconNode alloc] init] matchingToolbar:self.toolbar];
        [addPhotosBarButtonButtonNode addTarget:self action:@selector(tapAddPhotos:) forControlEvents:ASControlNodeEventTouchUpInside];
        self.toolbar.leftButtonsNode = addPhotosBarButtonButtonNode;
        [self.toolbar.defaultSendButton addTarget:self action:@selector(tapSend:) forControlEvents:ASControlNodeEventTouchUpInside];    
    
        // delegate must be self for interactive keyboard, datasource can be whatever
        self.node.tableNode.delegate = self;
        self.node.tableNode.dataSource = self;
        [self customizeCellFactory];
    }
    return self
}

- (void)customizeCellFactory {
    MXRMessageCellLayoutConfiguration* layoutConfigForMe = [MXRMessageCellLayoutConfiguration rightToLeft];
    MXRMessageCellLayoutConfiguration* layoutConfigForOthers = [MXRMessageCellLayoutConfiguration leftToRight];
    
    MXRMessageAvatarConfiguration* avatarConfigForMe = nil;
    MXRMessageAvatarConfiguration* avatarConfigForOthers = [[MXRMessageAvatarConfiguration alloc] init];
    
    MXRMessageTextConfiguration* textConfigForMe = [[MXRMessageTextConfiguration alloc] initWithFont:nil textColor:[UIColor whiteColor] backgroundColor:[UIColor mxr_fbMessengerBlue]];
    MXRMessageTextConfiguration* textConfigForOthers = [[MXRMessageTextConfiguration alloc] initWithFont:nil textColor:[UIColor blackColor] backgroundColor:[UIColor mxr_bubbleLightGrayColor]];
    CGFloat maxCornerRadius = textConfigForMe.maxCornerRadius;
    
    MXRMessageImageConfiguration* imageConfig = [[MXRMessageImageConfiguration alloc] init];
    imageConfig.maxCornerRadius = maxCornerRadius;
    MXRMessageMediaCollectionConfiguration* mediaCollectionConfig = [[MXRMessageMediaCollectionConfiguration alloc] init];
    mediaCollectionConfig.maxCornerRadius = maxCornerRadius;
    
    textConfigForMe.menuItemTypes |= MXRMessageMenuItemTypeDelete;
    textConfigForOthers.menuItemTypes |= MXRMessageMenuItemTypeDelete;
    imageConfig.menuItemTypes |= MXRMessageMenuItemTypeDelete;
    imageConfig.showsUIMenuControllerOnLongTap = YES;
    CGFloat s = [UIScreen mainScreen].scale;
    imageConfig.borderWidth = s > 0 ? (1.0f/s) : 0.5f;
    
    MXRMessageCellConfiguration* cellConfigForMe = [[MXRMessageCellConfiguration alloc] initWithLayoutConfig:layoutConfigForMe avatarConfig:avatarConfigForMe textConfig:textConfigForMe imageConfig:imageConfig mediaCollectionConfig:mediaCollectionConfig];
    MXRMessageCellConfiguration* cellConfigForOthers = [[MXRMessageCellConfiguration alloc] initWithLayoutConfig:layoutConfigForOthers avatarConfig:avatarConfigForOthers textConfig:textConfigForOthers imageConfig:imageConfig mediaCollectionConfig:mediaCollectionConfig];
    
    self.cellFactory = [[MXRMessageCellFactory alloc] initWithCellConfigForMe:cellConfigForMe cellConfigForOthers:cellConfigForOthers];
    self.cellFactory.dataSource = self;
    self.cellFactory.contentNodeDelegate = self;
    self.cellFactory.mediaCollectionDelegate = self;
}

要创建信息单元格,您可以实现如下 ASTableDataSource 方法

- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath {
    Message* message = self.messages[indexPath.row];
    if (message.media.count > 1) {
        return [self.cellFactory cellNodeBlockWithMedia:message.media tableNode:tableNode row:indexPath.row];
    } else if (message.media.count == 1) {
        MessageMedium* medium = message.media.firstObject;
        return [self.cellFactory cellNodeBlockWithImageURL:medium.photoURL showsPlayButton:(medium.videoURL != nil) tableNode:tableNode row:indexPath.row];
    } else {
        return [self.cellFactory cellNodeBlockWithText:message.text tableNode:tableNode row:indexPath.row];
    }
}

要发送新信息或更新带有新/旧信息的多米诺表格,只需在 cellFactory 上有一个方法。

- (void)updateTableNode:(ASTableNode*)tableNode animated:(BOOL)animated withInsertions:(NSArray<NSIndexPath*>*)insertions deletions:(NSArray<NSIndexPath*>*)deletions reloads:(NSArray<NSIndexPath*>*)reloads completion:(void(^)(BOOL))completion;

请记住以下几个关键假设

  • 只有一个部分:0。
  • 该表是 逆序,所以将新信息插入到(0,0)。
  • 对于时间间隔大于15分钟的空白信息,会自动显示时间戳头部,但如果您想要禁用此功能,则可以这样做。您可以通过 cellFactory 上的方法编程地显示/隐藏它们(例如,当用户点击单元格时)。
  • 圆角处理是通过调用与当前 一样以及其邻居的 MXRMessageCellFactoryDataSource 方法来进行的。这意味着您应该保持这些方法快速 - 理想情况下,它们只是字典查找。
  • 在点击时显示媒体不在本库的范围内。我有一个基于Texture的媒体查看器,我正在使用它,并希望有一天能将其开源。

有关更多详细信息,最好检查示例项目。

示例

要运行示例项目,请克隆仓储库,并在包含 Podfile 的 Example 目录中运行 pod install。然后打开 *.xcworkspace 文件并进行构建。

要求

Texture 2.X 和 iOS 8+。

贡献

目前没有测试。请确保PR保持非常小。

作者

Scott Kensell,[email prot

许可协议

MXRMessenger 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。