Skip to content

Commit 79a6b56

Browse files
author
mengyaoyao
committed
普通文本,表情消息发送UI
1 parent 372ad67 commit 79a6b56

File tree

14 files changed

+365
-61
lines changed

14 files changed

+365
-61
lines changed

.DS_Store

-4 KB
Binary file not shown.

CocoaAsyncSocket_TCP/ChatHandler/ChatHandler.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ - (void)socketDidSecure:(GCDAsyncSocket *)sock
276276
//此版本号需和后台协商 , 便于后台进行版本控制
277277
loginModel.versionCode = TCP_VersionCode;
278278
//当前用户ID
279-
loginModel.fromUserID = [Account shareInstance].myUserID;
279+
loginModel.fromUserID = [Account account].myUserID;
280280
//设备类型
281281
loginModel.deviceType = DeviceType;
282282
//发送登录验证

CocoaAsyncSocket_TCP/ChatHandler/ChatUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
//消息高度计算
1616
+ (CGFloat)heightForMessage:(ChatModel *)currentChatmodel premodel:(ChatModel *)premodel;
1717

18+
//创建消息模型
19+
+ (ChatModel *)creatMessageModel;
1820
@end

CocoaAsyncSocket_TCP/ChatHandler/ChatUtil.m

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "ChatUtil.h"
1010
#import "ChatModel.h"
11+
#import "MYCoreTextLabel.h"
1112

1213
@implementation ChatUtil
1314

@@ -36,30 +37,69 @@ + (CGFloat)heightForMessage:(ChatModel *)currentChatmodel premodel:(ChatModel *)
3637
CGFloat height = 0.f;
3738
//文本,表情
3839
if (hashEqual(currentChatmodel.contenType, Content_Text)) {
39-
40-
return currentChatmodel.messageHeight = currentChatmodel.shouldShowTime ? height + 40 : height;
40+
MYCoreTextLabel *coreLabel = [[MYCoreTextLabel alloc]init];
41+
coreLabel.textFont = FontSet(14);
42+
coreLabel.lineSpacing = 6;
43+
coreLabel.imageSize =CGSizeMake(24, 24);
44+
[coreLabel setText:currentChatmodel.content.text customLinks:nil keywords:nil];
45+
CGSize labelSize = [coreLabel sizeThatFits:CGSizeMake(SCREEN_WITDTH - 145, MAXFLOAT)];
46+
height = 5 + 10 + labelSize.height + 10;
47+
//验证是否群聊
48+
[self groupChatConfig:currentChatmodel];
49+
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15;
4150
//语音
4251
}else if (hashEqual(currentChatmodel.contenType, Content_Audio)){
4352

44-
return currentChatmodel.messageHeight = currentChatmodel.shouldShowTime ? height + 40 : height;
53+
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15;
4554
//图片
4655
}else if (hashEqual(currentChatmodel.contenType, Content_Picture)){
4756

48-
return currentChatmodel.messageHeight = currentChatmodel.shouldShowTime ? height + 40 : height;
57+
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15;
4958
//视频
5059
}else if (hashEqual(currentChatmodel.contenType, Content_Video)){
5160

52-
return currentChatmodel.messageHeight = currentChatmodel.shouldShowTime ? height + 40 : height;
61+
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15;
5362
//文件
5463
}else if (hashEqual(currentChatmodel.contenType, Content_File)){
5564

56-
return currentChatmodel.messageHeight = currentChatmodel.shouldShowTime ? height + 40 : height;
65+
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15;
5766
//提示语
5867
}else{
5968

60-
return currentChatmodel.messageHeight = currentChatmodel.shouldShowTime ? height + 40 : height;;
69+
return currentChatmodel.messageHeight += currentChatmodel.shouldShowTime ? height + 50 : height + 15;
70+
}
71+
}
72+
73+
//群聊特殊布局
74+
+ (void)groupChatConfig:(ChatModel *)chatModel
75+
{
76+
if (hashEqual(chatModel.chatType, @"groupChat")&&!chatModel.byMyself.integerValue) {
77+
chatModel.messageHeight += 16;
6178
}
6279
}
6380

6481

82+
#pragma marl - 创建发送消息模型
83+
+ (ChatModel *)creatMessageModel
84+
{
85+
ChatModel *messageModel = [[ChatModel alloc]init];
86+
ChatContentModel *content = [[ChatContentModel alloc]init];
87+
messageModel.content = content;
88+
messageModel.fromUserID = [Account account].myUserID;
89+
messageModel.toUserID = nil;
90+
messageModel.messageType = @"normal";
91+
messageModel.deviceType = @"iOS";
92+
messageModel.versionCode = TCP_VersionCode;
93+
messageModel.byMyself = @1;
94+
messageModel.isSend = @1;
95+
messageModel.isRead = @0;
96+
messageModel.beatID = TCP_beatBody;
97+
messageModel.fromPortrait = [Account account].portrait;
98+
messageModel.toNickName = nil;
99+
messageModel.noDisturb = nil;
100+
101+
return messageModel;
102+
}
103+
104+
65105
@end

CocoaAsyncSocket_TCP/ChatModel/ChatModel.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ typedef NS_ENUM(NSInteger){
6363

6464
@property (nonatomic, copy) NSString *groupID; //群ID
6565

66-
@property (nonatomic, copy) NSString *userID; //好友ID
67-
6866
@property (nonatomic, copy) NSString *fromUserID; //消息发送者ID
6967

68+
@property (nonatomic, copy) NSString *toUserID; //对方ID
69+
70+
@property (nonatomic, copy) NSString *fromPortrait; //发送者头像url
71+
72+
@property (nonatomic, copy) NSString *toPortrait; //对方头像url
73+
74+
@property (nonatomic, copy) NSString *toNickName; //我对好友命名的昵称
75+
7076
@property (nonatomic, copy) NSArray<NSString *> *atToUserIDs; // @目标ID
7177

7278
@property (nonatomic, copy) NSString *messageType; //消息类型
@@ -91,16 +97,13 @@ typedef NS_ENUM(NSInteger){
9197

9298
@property (nonatomic, copy) NSString *beatID; //心跳标识
9399

94-
@property (nonatomic, copy) NSString *portrait; //头像url
95-
96100
@property (nonatomic, copy) NSString *groupName; //群名称
97101

98-
@property (nonatomic, copy) NSString *nickName; //好友昵称
99-
100102
@property (nonatomic, strong) NSNumber *noDisturb; //免打扰状态 , 1为正常接收 , 2为免打扰状态 , 3为屏蔽状态
101103

102104
@property (nonatomic, strong) ChatContentModel *content; //内容
103105

106+
@property (nonatomic, strong) NSNumber *isSending; //是否正在发送中
104107

105108
#pragma mark - chatlist独有部分
106109
@property (nonatomic, strong) NSNumber *unreadCount; //未读数

CocoaAsyncSocket_TCP/Comon/Account/Account.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838

3939
@property (nonatomic ,copy) NSString *lastLoginTime; //最后登录时间
4040

41+
@property (nonatomic, copy) NSString *portrait; //头像url
42+
4143
/*
4244
这里仅仅是一个模拟 , 真正的关于当前用户的资料可能还会有很多
4345
*/
4446

45-
+ (instancetype)shareInstance;
47+
+ (instancetype)account;
4648

4749
@end

CocoaAsyncSocket_TCP/Comon/Account/Account.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ - (void)setValue:(id)value forUndefinedKey:(NSString *)key
1414
{
1515
}
1616

17-
+ (instancetype)shareInstance
17+
+ (instancetype)account
1818
{
1919
static Account *account = nil;
2020
static dispatch_once_t onceToken;

CocoaAsyncSocket_TCP/Controller/ChatListViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ - (void)sendMessageTimeOutWithTag:(long)tag
125125
- (void)getMessages
126126
{
127127
//暂时先模拟假数据 , 后面加上数据库结构,再修改
128-
NSArray *tips = @[@"项目里IM这块因为一直都是在摸索,所以特别乱..",@"这一份相当于是进行重构,分层,尽量减少耦合性",@"还有就是把注释和大体思路尽量写下来",@"UI部分很耗时,因为所有的东西都是自己写的",@"如果有兴趣可以fork一下,有空闲时间我就会更新一些",@"如果觉得有用,麻烦star一下噢...."];
128+
NSArray *tips = @[@"项目里IM这块因为一直都是在摸索,所以特别乱..",@"这一份相当于是进行重构,分层,尽量减少耦合性",@"还有就是把注释和大体思路尽量写下来",@"UI部分很耗时,因为所有的东西都是自己写的",@"如果有兴趣可以fork一下,有空闲时间我就会更新一些",@"如果觉得有用,麻烦star一下噢....",@"如果觉得有用,麻烦star一下噢....",@"具体IP端口涉及公司隐私东西已经隐藏....",@"具体IP端口涉及公司隐私东西已经隐藏...."];
129129
for (NSInteger index = 0; index < 30; index ++) {
130130

131131
ChatModel *chatModel = [[ChatModel alloc]init];
132132
ChatContentModel *chatContent = [[ChatContentModel alloc]init];
133133
chatModel.content = chatContent ;
134-
chatModel.nickName = @"孟遥";
134+
chatModel.toNickName = @"孟遥";
135135
if (index<tips.count) {
136136
chatModel.lastMessage = tips[index];
137137
}else{

CocoaAsyncSocket_TCP/Controller/ChatViewController.m

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,24 @@ - (ChatKeyboard *)customKeyboard
3939
if (!_customKeyboard) {
4040
_customKeyboard = [[ChatKeyboard alloc]init];
4141
//传入当前控制器 ,方便打开相册(如放到控制器 , 后期的逻辑过多,控制器会更加臃肿)
42-
_customKeyboard.target = self;
42+
__weak typeof(self) weakself = self;
43+
//普通文本消息
44+
[_customKeyboard textCallback:^(ChatModel *textModel) {
45+
46+
[weakself sendTextMessage:textModel];
47+
//语音消息
48+
} audioCallback:^(ChatModel *audioModel) {
49+
50+
[weakself sendAudioMessage:audioModel];
51+
//图片消息
52+
} picCallback:^(ChatModel *picModel) {
53+
54+
[weakself sendPictureMessage:picModel];
55+
//视频消息
56+
} videoCallback:^(ChatModel *videoModel) {
57+
58+
[weakself sendVideoMessage:videoModel];
59+
} target:self];
4360
}
4461
return _customKeyboard;
4562
}
@@ -77,6 +94,8 @@ - (UITableView *)chatTableView
7794
{
7895
if (!_chatTableView) {
7996
_chatTableView = [[UITableView alloc]initWithFrame:Frame(0, 0, SCREEN_WITDTH, Height(self.view.bounds)-49) style:UITableViewStylePlain];
97+
_chatTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
98+
_chatTableView.allowsSelection = NO;
8099
_chatTableView.delegate = self;
81100
_chatTableView.dataSource = self;
82101
//普通文本,表情消息类型
@@ -104,6 +123,15 @@ - (void)viewDidLoad {
104123
[self getHistoryMessages];
105124
}
106125

126+
#pragma mark - 注册通知
127+
- (void)viewWillAppear:(BOOL)animated
128+
{
129+
[super viewWillAppear:animated];
130+
//键盘弹起通知
131+
[[NSNotificationCenter defaultCenter]addObserver:self.customKeyboard selector:@selector(systemKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
132+
133+
}
134+
107135
#pragma mark - dataSource
108136

109137
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
@@ -114,12 +142,11 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
114142
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
115143
{
116144
ChatModel *chatModel = self.talkMessages[indexPath.row];
117-
118145
//文本,表情消息
119146
if (hashEqual(chatModel.contenType, Content_Text)) {
120147

121148
ChatTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"ChatTextCell"];
122-
149+
textCell.textModel = chatModel;
123150
return textCell;
124151

125152
//语音消息
@@ -159,18 +186,6 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
159186
}
160187
}
161188

162-
#pragma mark - delegate
163-
164-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
165-
{
166-
ChatModel *chatmodel = self.talkMessages[indexPath.row];
167-
ChatModel *premodel = nil;
168-
if (self.talkMessages.count > 1) premodel = self.talkMessages[self.talkMessages.count - 2];
169-
//如果已经计算过 , 直接返回高度
170-
if (chatmodel.messageHeight) return chatmodel.messageHeight;
171-
//计算消息高度
172-
return [ChatUtil heightForMessage:chatmodel premodel:premodel];
173-
}
174189

175190
#pragma mark - 拉取数据库消息
176191
- (void)getHistoryMessages
@@ -182,7 +197,7 @@ - (void)getHistoryMessages
182197
- (void)initUI
183198
{
184199
//初始化导航
185-
self.titleView.text = [_chatModel.chatType isEqualToString:@"groupChat"] ? _chatModel.groupName : _chatModel.nickName;
200+
self.titleView.text = [_chatModel.chatType isEqualToString:@"groupChat"] ? _chatModel.groupName : _chatModel.toNickName;
186201
self.navigationItem.titleView = self.titleView;
187202
CGSize titleSize = [self.titleView.text sizeWithFont:self.titleView.font maxSize:CGSizeMake(200,16)];
188203
//正常接收消息状态
@@ -193,21 +208,38 @@ - (void)initUI
193208
self.titleView.bounds = Frame(0, 0, titleSize.width + 5 + 14, 16);
194209
self.bellView.frame = Frame(titleSize.width + 5, (Height(self.titleView.frame)-14)*0.5, 14, 14);
195210
}
196-
197211
//初始化聊天界面
198212
[self.view addSubview:self.chatTableView];
199-
200213
//初始化键盘
201214
[self.view addSubview:self.customKeyboard];
202215
self.customKeyboard.frame = Frame(0, SCREEN_HEIGHT - 49, SCREEN_WITDTH, CTKEYBOARD_DEFAULTHEIGHT);
203216
}
204217

205-
#pragma mark - 注册通知
206-
- (void)viewWillAppear:(BOOL)animated
218+
219+
#pragma mark - 发送文本/表情消息
220+
- (void)sendTextMessage:(ChatModel *)textModel
221+
{
222+
[self.talkMessages addObject:textModel];
223+
[self.chatTableView reloadData];
224+
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_talkMessages.count - 1 inSection:0];
225+
[self.chatTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
226+
}
227+
228+
#pragma mark - 发送语音消息
229+
- (void)sendAudioMessage:(ChatModel *)audioModel
230+
{
231+
232+
}
233+
234+
#pragma mark - 发送图片消息
235+
- (void)sendPictureMessage:(ChatModel *)picModel
236+
{
237+
238+
}
239+
240+
#pragma mark - 发送视频消息
241+
- (void)sendVideoMessage:(ChatModel *)videoModel
207242
{
208-
[super viewWillAppear:animated];
209-
//键盘弹起通知
210-
[[NSNotificationCenter defaultCenter]addObserver:self.customKeyboard selector:@selector(systemKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
211243

212244
}
213245

@@ -217,4 +249,16 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
217249

218250
}
219251

252+
#pragma mark - delegate
253+
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
254+
{
255+
ChatModel *chatmodel = self.talkMessages[indexPath.row];
256+
ChatModel *premodel = nil;
257+
if (self.talkMessages.count > 1) premodel = self.talkMessages[self.talkMessages.count - 2];
258+
//如果已经计算过 , 直接返回高度
259+
if (chatmodel.messageHeight) return chatmodel.messageHeight;
260+
//计算消息高度
261+
return [ChatUtil heightForMessage:chatmodel premodel:premodel];
262+
}
263+
220264
@end

CocoaAsyncSocket_TCP/View/ChatCells/ChatTextCell.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
//
88

99
#import <UIKit/UIKit.h>
10+
@class ChatModel;
1011

1112
@interface ChatTextCell : UITableViewCell
1213

14+
@property (nonatomic, strong) ChatModel *textModel;
15+
1316
@end

0 commit comments

Comments
 (0)