作者:孟令文
//在遇到有輸入的情況下。由于現(xiàn)在鍵盤的高度是動態(tài)變化的。中文輸入與英文輸入時高度不同栗涂。所以輸入框的位置也要做出相應(yīng)的變化
#pragma mark - keyboardHight
-(void)viewWillAppear:(BOOL)animated
{
[self registerForKeyboardNotifications];
}
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)registerForKeyboardNotifications
{
//使用NSNotificationCenter 鍵盤出現(xiàn)時
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
//使用NSNotificationCenter 鍵盤隱藏時
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
//實現(xiàn)當(dāng)鍵盤出現(xiàn)的時候計算鍵盤的高度大小瞧掺。用于輸入框顯示位置
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
NSLog(@"hight_hitht:%f",kbSize.height);
if(kbSize.height == 216)
{
keyboardhight = 0;
}
else
{
keyboardhight = 36;? //252 - 216 系統(tǒng)鍵盤的兩個不同高度
}
//輸入框位置動畫加載
[self begainMoveUpAnimation:keyboardhight];
}
//當(dāng)鍵盤隱藏的時候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
//do something
}
//(TextView) 當(dāng)鍵盤開始輸入前案训。時行計算與動畫加載
-(void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"gegin animation");
sendMsgTextView =textView;
resultCommunityTableview.frame = CGRectMake(0, 36, 320, 150);
//動畫加載
[self begainMoveUpAnimation:0.0 ];
}
//關(guān)閉鍵盤(TextView) 換行時。隱藏鍵盤
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
resultCommunityTableview.frame = CGRectMake(0, 36, 320, 376);
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
//輸入結(jié)束時調(diào)用動畫(把按鍵速警。背景叹誉。輸入框都移下去)
-(void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@"tabtabtab");
[self endEditAnimation];
//釋放
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
//判斷當(dāng)前輸入法
-(void)textViewDidChangeSelection:(UITextView *)textView
{
NSLog(@"wewe:%@",[[UITextInputMode currentInputMode] primaryLanguage]);
/*
if ([[UITextInputMode currentInputMode] primaryLanguage] == @"en-US") {
NSLog(@"en-US");
}
else
{
NSLog(@"zh-hans");
}
*/
}