UItextView通常作为文本输入框,想信息里和微信里的输入框,那么怎么实现UItextView的高度随字数而改变,下面是我自己研究的一个方法,亲测可用哦!
首先先把这次项目里面需要用到的成员变量列出来
{
UIView *_bgView;
UITextView *_textView;
UILabel *_PlaLabel;
BOOL _isKeyBoardShow;//记录键盘是否出现
CGFloat _bgOriginY;//记录_bgView的y值
}```
一、先写个简单的,给UItextView添加一个占位符吧!
1、初始化一个UItextView
-
(void)addTextView {
self.automaticallyAdjustsScrollViewInsets = NO;
_bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight-50, kScreenWidth, 50)];
_bgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_bgView];_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, kScreenWidth-20-60-8, 30)];
_textView.delegate = self;
_textView.returnKeyType = UIReturnKeySend;
_textView.backgroundColor = [UIColor whiteColor];
_textView.font = [UIFont systemFontOfSize:14];
[CommonLib layerBorderWidth:0.5 borderColor:ILColor(238, 238, 238).CGColor cornerRadius:2 forView:_textView];
[_bgView addSubview:_textView];
}```
2、创建一个label作为占位符
//给textView加一个占位符
- (void)addPlaceholder {
_PlaLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 60, 20)];
_PlaLabel.textColor = ILColor(218, 218, 218);
_PlaLabel.text = @"请输入";
_PlaLabel.hidden = NO;
_PlaLabel.font = [UIFont systemFontOfSize:14];
[_textView addSubview:_PlaLabel];
}```
3、在代理方法中判断占位符的出现和隐藏
-
(void)textViewDidChange:(UITextView *)textView {
if (textView.text.length == 0) {
_PlaLabel.hidden = NO;
} else {
_PlaLabel.hidden = YES;
}
//上面这个if else语句可以用三目运算符写!如下
_PlaLabel.hidden = textView.text.length == 0 ? NO : YES;
}```
好了下面来讲UItextView自动高度
二、UItextView自动高度
1、注册通知,在viewdidload方法里面添加键盘通知
//在viewdidload里面注册通知 键盘出现
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
//键盘消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//把记录键盘的bool值置NO
_isKeyBoardShow = NO;```
2、销毁通知,注册了通知之后一定要销毁,在viewDidDisappear里面销毁通知
[[NSNotificationCenter defaultCenter] removeObserver:self];```
3、实现键盘出现的通知,这里实现了输入框随键盘移动而移动
- (void)keyboardWillChangeFrame:(NSNotification *)notification {
_isKeyBoardShow = YES;
NSDictionary *userInfo = notification.userInfo;
// 动画的持续时间
double duration = [userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 键盘的frame
CGRect keyboardF = [userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect bgFrame = _bgView.frame;
//计算_bgView的y值,键盘的y值 - _bgView的高
bgFrame.origin.y = keyboardF.origin.y - bgFrame.size.height;
//只在第一次获取
if (!_bgOriginY) {
_bgOriginY = bgFrame.origin.y;
}
// 执行动画
[UIView animateWithDuration:duration animations:^{
_bgView.frame = bgFrame;
}];
}```
4、实现键盘消失的通知
5、在代理方法里面实现,键盘高度自动化
-
(void)textViewDidChange:(UITextView *)textView {
_PlaLabel.hidden = textView.text.length == 0 ? NO : YES;
CGFloat height = [CommonLib heightLabelText:_textView.text width:kScreenWidth-20-60-8-10 font:14];//这个方法是根据字数自动计算高度的,会在文章最后附上!减去10的左右间距,这里的10是指UItextView本身自带的左右间距
CGRect bgframe = _bgView.frame;
CGRect textFrame = _textView.frame;//如果高度小于30,即UItextView本身的高度,那么就把高度这只成本来的高度,这里主要是在两行字删除到只有一行的时候调用
if (height < 30) {_textView.contentSize = CGSizeMake(0, 30);//这句话很重要哦,不设置移动范围的话你会发现你的占位符会乱跑!!而且只能写在这里,写在别的地方你就动不了你的textView了! bgframe.size.height = 30+20; bgframe.origin.y = _bgOriginY; textFrame.size.height = 30; [UIView animateWithDuration:0.2 animations:^{ _bgView.frame = bgframe; _textView.frame = textFrame; }];
//我们在这里定义了一个UITextView最大可以达到的高度67,这个高度可以随便设置,当超出了67后,高度将不再变化
} else if (height > 30 && height < 67) {bgframe.size.height = height+20+16; bgframe.origin.y = _bgOriginY-height-16+30; textFrame.size.height = height+16; //添加动画,使高度变化看起来连贯流畅!!! [UIView animateWithDuration:0.2 animations:^{ _bgView.frame = bgframe; _textView.frame = textFrame; }];
}
}```
6、放下键盘,在touch方法里面实现,点击空白处放下键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//这里不需要写输入框高度变化,系统会监听到键盘的高度变化
_isKeyBoardShow = NO;
[_textView resignFirstResponder];
}```
7、附上上面用到的两个封装的类方法,这里写成私有方法,自己把上面的CommonLib换成self哦!
//计算高度
-
(CGFloat)heightLabelText:(NSString *)text width:(CGFloat)width font:(CGFloat)font {
NSInteger w = width;
return contentH;
}```
//绘制UIView的边线
- (void)layerBorderWidth:(CGFloat)borderWidth borderColor:(CGColorRef)borderColor cornerRadius:(CGFloat)cornerRadius forView:(UIView *)view {
view.layer.masksToBounds = YES;//超出部分截掉
view.layer.borderWidth = borderWidth;//边线宽度
view.layer.borderColor = borderColor;//边线颜色
view.layer.cornerRadius = cornerRadius;//边框角度
}```
写个博客不容易,大家看了喜欢的话点个赞哦!有什么想法或者bug希望大家可以和我多多交流哈!!!