//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
//获取键盘类型高度
- (void)keyboardWillShown:(NSNotification*)aNotification{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
keyboardHeight = keyboardRect.size.height;
// _spacingWithKeyboardAndCursor = keyboardHeight - _cursorHeight;
// if (_spacingWithKeyboardAndCursor > 0) {
// [UIView animateWithDuration:kAnimationDuration animations:^{
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
self.view.frame = CGRectMake(0.0f, -(keyboardHeight - 65), self.view.frame.size.width, self.view.frame.size.height);
// }];
// }
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification {
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
self.view.frame = CGRectMake(0.0f, 65, self.view.frame.size.width, self.view.frame.size.height);
}
-(void)textViewDidChange:(UITextView *)textView
{
if (self.comment == textView) {
float currentLineNum=1;//默认文本框显示一行文字
float textViewWidth=self.comment.frame.size.width;//取得文本框高度
NSString *content=textView.text;
NSDictionary *dict=@{NSFontAttributeName:[UIFont systemFontOfSize:15.0 * ScreenWidth]};
CGSize contentSize=[content sizeWithAttributes:dict];//计算文字长度
float numLine=ceilf(contentSize.width/textViewWidth); //计算当前文字长度对应的行数
if(numLine > currentLineNum ){
//如果发现当前文字长度对应的行数超过。 文本框高度,则先调整当前view的高度和位置,然后调整输入框的高度,最后修改currentLineNum的值
self.backG.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 110 * ScreenHeigth + 15 * ScreenHeigth * (currentLineNum-numLine), [UIScreen mainScreen].bounds.size.width, 45 * ScreenHeigth + 15 * ScreenHeigth *(numLine-currentLineNum));
//
self.comment.frame = CGRectMake(self.comment.frame.origin.x, 5 * ScreenHeigth, self.comment.frame.size.width, 35 * ScreenHeigth + 15 * ScreenHeigth *(numLine-currentLineNum));
self.view.frame = CGRectMake(0.0f, -(keyboardHeight - 65 ), self.view.frame.size.width, self.view.frame.size.height);
//textView.frame=CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, 26 + 26*(numLine-currentLineNum));
currentLineNum=numLine;
[self.view addSubview:self.backG];
[self.backG addSubview:self.comment];
[self.backG addSubview:self.send];
}else if (numLine - currentLineNum > 0 ){
//次数为删除的时候检测文字行数减少的时候
self.backG.frame = CGRectMake(0, self.backG.frame.origin.y - 15 * ScreenHeigth * (currentLineNum-numLine), [UIScreen mainScreen].bounds.size.width, self.backG.frame.size.height - 15 * ScreenHeigth *(numLine-currentLineNum));
self.comment.frame = CGRectMake(self.comment.frame.origin.x, 5 * ScreenHeigth, self.comment.frame.size.width, self.comment.frame.size.height - 15 * ScreenHeigth * (currentLineNum-numLine));
// textView.frame=CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.frame.size.height-20*(currentLineNum-numLine));
[self.view addSubview:self.backG];
[self.backG addSubview:self.comment];
currentLineNum=numLine;
}
}
}