iOS 自定义一个textView,可做聊天输入框,cell中也可以自适应

前言

项目中经常会用到UITextView,但是系统的textView在限制多行输入的时候会出现问题。(过渡动画很乱)。基于限制多行输入或者cell跟随textView内容自适应的需求,所以自定义一个textView

自定义textView

关于textView的一些摸索,iOS仿微信输入框,限制最大行数,一体键盘(两种思路),这篇文章中有介绍,下面示例会用到。

封装一个可限制多行输入的textView,首先屏蔽掉系统对textView的默认设置_textView.textContainerInset = UIEdgeInsetsZero,屏蔽掉之后可以给textView添加一个上下左右的间距,UIView作为textView的容器。这样在限制多上输入的时候动画过度就会很自然,默认是不限制行数的。

.h接口的配置:

1
2
3
4
5
6
7
8
9
@property(nonatomic,assign)CGFloat v_margin;//竖直方向上下间距 默认为8;
@property(nonatomic,assign)CGFloat h_margin;//水平方向上下间距 默认为0;
@property(nonatomic,assign)NSInteger initiLine;//初始需要展示的行数 默认为1;
@property(nonatomic,assign)NSInteger maxLine;//最大行数 默认为无穷大;
@property(nonatomic,strong)NSString *placeholder;//占位文字
@property(nonatomic,strong)UIFont *font;//默认为17
@property(nonatomic,assign)CGPoint placePoint;//设置占位符的位置,竖直方向设置v_margin即可 CGPointMake(5, 0);//占位文字的起始位置;
@property(nonatomic,copy)void (^textHeightChangeBlock)(CGFloat height);

#####textView需要一个默认的初始行数,默认是1,所以后续自定义的输入框是要根据行数来确定高度,随便给高度,动画就很尴尬。

textView已经屏蔽掉了iOS11textView作为UIScrollView子类的处理:

1
2
3
if (@available(iOS 11.0, *)) {
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

关于限制行数的处理:

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
-(void)textViewDidChange:(UITextView *)textView{
//内容高度
CGFloat contentSizeH = self.textView.contentSize.height;
//最大高度
CGFloat maxHeight = ceil(self.font.lineHeight * self.maxLine);
//初始高度
CGFloat initiTextViewHeight = ceilf(self.initiLine *self.font.lineHeight);
if (contentSizeH <= maxHeight) {
if (contentSizeH <= initiTextViewHeight) {
self.textView.height = initiTextViewHeight;
}else{
self.textView.height = contentSizeH;
}
}else{
self.textView.height = maxHeight;
}
self.height = self.textView.height + 2 * self.v_margin;
if (self.textHeightChangeBlock) {
self.textHeightChangeBlock(self.height);
}
[textView scrollRangeToVisible:NSMakeRange(textView.selectedRange.location, 1)];
}

#####支持约束以及frame配置。

示例1 自定义一体键盘,限制最大行数。

很简单的在xib底部关联一个自定义的textView,需要监测两个通知UIKeyboardWillChangeFrameNotificatioUIKeyboardDidChangeFrameNotification,至于为什么要监听,前面那篇文章有详细说明。补充一点,如果项目中有使用到IQKeyboardManager,需要在当前页面设置IQKeyboardManager不可用,在自定义键盘的时候如果IQKeyboardManager开始,可能会出现导航栏突然消失很多问题,所以关闭掉,监听两个通知即可,改变自定义键盘 底部距离屏幕底部的约束即可,键盘弹出,高于键盘即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.isDisappear = NO;
[IQKeyboardManager sharedManager].enable = NO; //关闭
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.isDisappear = YES;
[IQKeyboardManager sharedManager].enable = YES; //开启
}
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
-(void)keyboardWillChangeFrame:(NSNotification *)notification{
NSDictionary *userInfo = notification.userInfo;
// 动画的持续时间
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 键盘的frame
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// NSLog(@"%@",NSStyringFromCGRect(keyboardF));
CGFloat keyboardY = keyboardF.origin.y;
keyboardH = keyboardF.size.height;
if (!self.isDisappear) {
[self dealKeyBoardWithKeyboardH:keyboardH keyboardY:keyboardY duration:duration];
}
}
-(void)keyboardDidChangeFrame:(NSNotification *)notification{
NSDictionary *userInfo = notification.userInfo;
// 动画的持续时间
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardY = keyboardF.origin.y;
keyboardH = keyboardF.size.height;
if (self.isDisappear) {
[self dealKeyBoardWithKeyboardH:keyboardH keyboardY:keyboardY duration:duration];
}
}
#pragma mark---处理高度---
-(void)dealKeyBoardWithKeyboardH:(CGFloat)keyboardH keyboardY:(CGFloat)keyboardY duration:(CGFloat)duration{
if (!self.isDisappear) {
[UIView animateWithDuration:duration animations:^{
// 工具条的Y值 == 键盘的Y值 - 工具条的高度
if (keyboardY >= Device_Height) {
self.bottomH.constant = 0;
}else
{
self.bottomH.constant = keyboardH;
}
}];
}else{
if (keyboardY >= Device_Height) {
self.bottomH.constant = 0;
}else
{
self.bottomH.constant = keyboardH;
}
}
}

#####如果在键盘弹出的时候同时textView需要自动增长,那么也需要设置如下:

1
2
3
4
5
6
7
8
9
10
self.textView.textHeightChangeBlock = ^(CGFloat height) {
weakSelf.textH.constant = height;
weakSelf.bottomH.constant = keyboardH;
[weakSelf.view updateConstraints];
[weakSelf.view updateConstraintsIfNeeded];
[UIView animateWithDuration:0.25 animations:^{
[weakSelf.view layoutIfNeeded];
}];
};

示例2 cell跟随textView自适应,textView也可以限制多行

需要做的也很简单,在textView自动增长的地方,处理如下:(当前textView不能失去焦点)

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
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.textView.initiLine = 1;
self.textView.maxLine = 3;
self.textView.placeholder= @"请输入";
self.textViewH.constant = self.textView.height;
LXWS(weakSelf);
self.textView.textHeightChangeBlock = ^(CGFloat height) {
weakSelf.textViewH.constant = height;
[weakSelf.contentView updateConstraints];
[weakSelf.contentView updateConstraintsIfNeeded];
[UIView animateWithDuration:0.25 animations:^{
[weakSelf.contentView layoutIfNeeded];
}];
UITableView *tableView = [weakSelf tableView];
[tableView beginUpdates];
[tableView endUpdates];
};

效果图:textView.gif

demo: LXCustomTextView