1,UITextview使用键盘上得return按钮进行退出键盘,限制输入个数;
2,使用UIKeyboardWillShowNotification通知,进行监控键盘是否弹出,根据键盘的高度,自动调整UItextView的高度,实现动画效果.原理比较简单,直接上代码.哈哈
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//退出键盘
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
//限制字数为300个
if (range.location >= 300 ) {
return NO; // return NO to not change text
}
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyboardTop = keyboardRect.origin.y;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
CGFloat newViewY = keyboardTop - _resuseReasonView.frame.size.height;
_resuseReasonView.frame = CGRectMake(_resuseReasonView.frame.origin.x, newViewY, _resuseReasonView.frame.size.width, _resuseReasonView.frame.size.height);
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
CGFloat resuseReasonViewX = 15;
CGFloat resuseReasonViewH = kScreenHeight / 2;
CGFloat resuseReasonViewY = (kScreenHeight - resuseReasonViewH) / 2;
CGFloat resuseReasonViewW = kScreenWidth - resuseReasonViewX * 2;
_resuseReasonView.frame = CGRectMake(resuseReasonViewX, resuseReasonViewY, resuseReasonViewW, resuseReasonViewH);
[UIView commitAnimations];
}