-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBarContainer.m
More file actions
180 lines (166 loc) · 6.6 KB
/
ChatBarContainer.m
File metadata and controls
180 lines (166 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// ChatBarContainer.m
// Hrland
//
// Created by Tousan on 15/9/18.
// Copyright (c) 2015年 Tousan. All rights reserved.
//
#import "ChatBarContainer.h"
#define kChatBarHeight
@implementation ChatBarContainer
{
NSString *send_Str;
}
- (id)init;
{
self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
if (self)
{
_isTextRequired = YES;
_max_Count = 140;
_txtView = [[ChatBarTextView alloc]init];
_txtView.placeHolder = @"说点什么吧~";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[self addSubview:_txtView];
self.backgroundColor = [UIColor whiteColor];
_txtView.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1].CGColor;
_txtView.layer.cornerRadius = 5;
[_txtView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(10);
make.right.equalTo(self.mas_right).offset(-100);
make.top.equalTo(self.mas_top).offset(5);
CGRect textFrame=[[_txtView layoutManager]usedRectForTextContainer:[_txtView textContainer]];
make.height.offset(textFrame.size.height+17);
}];
[_txtView layoutIfNeeded];
CGRect self_Rect = self.frame;
self_Rect.size.height = _txtView.frame.size.height+10;
self.frame = self_Rect;
_txtView.delegate = self;
_send_Btn = [UIButton new];
[self addSubview:_send_Btn];
[_send_Btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_txtView.mas_right).offset(10);
make.right.equalTo(self.mas_right).offset(-10);
CGRect textFrame=[[_txtView layoutManager]usedRectForTextContainer:[_txtView textContainer]];
make.height.offset(textFrame.size.height+17);
make.bottom.equalTo(_txtView.mas_bottom);
}];
_send_Btn.layer.cornerRadius = 5;
_send_Btn.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
[_send_Btn setTitle:@"发送" forState:UIControlStateNormal];
[_send_Btn addTarget:self action:@selector(clickSend) forControlEvents:UIControlEventTouchUpInside];
_send_Btn.enabled = _isTextRequired?NO:YES;
UIView *segline = [UIView new];
[self addSubview:segline];
[segline mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left);
make.right.equalTo(self.mas_right);
make.top.equalTo(self.mas_top);
make.height.offset(0.5);
}];
segline.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
//监听键盘出现事件
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWasShown)
name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (void)setIsTextRequired:(BOOL)isTextRequired;
{
_isTextRequired = isTextRequired;
if (!isTextRequired)
{
_send_Btn.enabled = YES;
_send_Btn.layer.backgroundColor = [UIColor colorWithRed:28/255.0 green:106/255.0 blue:255/255.0 alpha:1].CGColor;
}
}
- (void)keyboardWasShown
{
_txtView.placeHolder = @"";
}
- (void)textViewDidChange:(UITextView *)textView;
{
if ([textView positionFromPosition:[textView markedTextRange].start offset:0])
{
return;
}
send_Str = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
_send_Btn.enabled = _isTextRequired?(send_Str.length?YES:NO):YES;
_send_Btn.layer.backgroundColor = _send_Btn.isEnabled?[UIColor colorWithRed:28/255.0 green:106/255.0 blue:255/255.0 alpha:1].CGColor:[UIColor lightGrayColor].CGColor;
if (textView.text.length)
{
ChatBarTextView *txt_View = (ChatBarTextView*)textView;
txt_View.placeHolder_Label.hidden = YES;
}
else
{
ChatBarTextView *txt_View = (ChatBarTextView*)textView;
txt_View.placeHolder_Label.hidden = NO;
}
if (textView.text.length>_max_Count)
{
send_Str = [send_Str substringToIndex:_max_Count];
textView.text = send_Str;
}
CGRect textFrame = [[_txtView layoutManager]usedRectForTextContainer:[_txtView textContainer]];
if (textFrame.size.height<100)
{
[_txtView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.offset(textFrame.size.height+17);
}];
[_txtView setNeedsDisplay];
[_txtView layoutIfNeeded];
[self mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.offset(_txtView.frame.size.height+10);
}];
}
}
- (void)keyboardWillChange:(NSNotification *)aNotification
{
NSLog(@"Keyboard change");
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration animations:^{
[self mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.superview.mas_bottom).offset(-kbSize.height);
}];
[self layoutIfNeeded];
}];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSDictionary *info = [aNotification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration animations:^{
[self mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.superview.mas_bottom);
}];
[self layoutIfNeeded];
[self.superview layoutIfNeeded];
}completion:^(BOOL finished) {
if (_delegate && [_delegate respondsToSelector:@selector(chatBarDidEndEdit)])
{
[_delegate chatBarDidEndEdit];
}
}];
}
- (void)keyboardDidShow:(NSNotification *)aNotification
{
if (_delegate && [_delegate respondsToSelector:@selector(chatBarDidBecomeActive)])
{
[_delegate chatBarDidBecomeActive];
}
}
- (void)clickSend;
{
if (_delegate && [_delegate respondsToSelector:@selector(clickSendWithContent:)])
{
[_delegate clickSendWithContent:send_Str];
}
}
@end