iOS 獲得鍵盤的高度 NSNotificationCenter


//在遇到有輸入的情況下。由於現在鍵盤的高度是動態變化的。中文輸入與英文輸入時高度不同。所以輸入框的位置也要做出相應的變化
#pragma mark - keyboardHight
-(void)viewWillAppear:(BOOL)animated
{
    [self registerForKeyboardNotifications];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)registerForKeyboardNotifications
{
    //使用NSNotificationCenter 鍵盤出現時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShown:)
     
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    //使用NSNotificationCenter 鍵盤隱藏時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
     
    
}



//實現當鍵盤出現的時候計算鍵盤的高度大小。用於輸入框顯示位置
- (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 系統鍵盤的兩個不同高度
    }
    //輸入框位置動畫加載
    [self begainMoveUpAnimation:keyboardhight];
}

//當鍵盤隱藏的時候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
     //do something
}


//(TextView) 當鍵盤開始輸入前。時行計算與動畫加載
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"gegin animation");
    sendMsgTextView =textView;
    resultCommunityTableview.frame = CGRectMake(0, 36, 320, 150);
    //動畫加載
    [self begainMoveUpAnimation:0.0 ];
    
}


//關閉鍵盤(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;  
}  



//輸入結束時調用動畫(把按鍵。背景。輸入框都移下去)
-(void)textViewDidEndEditing:(UITextView *)textView
{
    NSLog(@"tabtabtab");
    [self endEditAnimation];
    
    //釋放
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}



//判斷當前輸入法
-(void)textViewDidChangeSelection:(UITextView *)textView
{
    NSLog(@"wewe:%@",[[UITextInputMode currentInputMode] primaryLanguage]);
    /*
    if ([[UITextInputMode currentInputMode] primaryLanguage] == @"en-US") { 
        NSLog(@"en-US"); 
    } 
    else 
    { 
        NSLog(@"zh-hans"); 
    } 
     */
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM