- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[textview resignFirstResponder];
return NO;
}
return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[textview resignFirstResponder];
}
-(void)viewWillAppear:(BOOL)animated{
//注冊通知,監(jiān)聽鍵盤彈出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
//注冊通知,監(jiān)聽鍵盤消失事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];
}
// 鍵盤彈出時
-(void)keyboardDidShow:(NSNotification *)notification
{
//獲取鍵盤高度
NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect;
[keyboardObject getValue:&keyboardRect];
//調(diào)整放置有textView的view的位置
//設(shè)置動畫
[UIView beginAnimations:nil context:nil];
//定義動畫時間
[UIView setAnimationDuration:5];
//設(shè)置view的frame壮虫,往上平移
[(UIView *)[self.view? viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-50, 320, 50)];
[UIView commitAnimations];
}
//鍵盤消失時
-(void)keyboardDidHidden
{
//定義動畫
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5];
//設(shè)置view的frame抛杨,往下平移
[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-50, 320, 50)];
[UIView commitAnimations];
}