彈出鍵盤textview

- (void)setTextView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 528, 320, 40)];
    view.backgroundColor = [UIColor lightGrayColor];
    view.tag = 100;
    [self.view addSubview:view];
    [view release];
    
    UIImageView *p_w_picpathView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 30, 30)];
    p_w_picpathView1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//設置p_w_picpathView1的自適應高度,和父視圖保持的下邊距保持不變
    //p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"[email protected]"];
    p_w_picpathView1.layer.cornerRadius = 15;
    p_w_picpathView1.clipsToBounds = YES;
    [view addSubview:p_w_picpathView1];
    [p_w_picpathView1 release];
    
    UIImageView *p_w_picpathView2 = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 30)];
    p_w_picpathView2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    //p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"[email protected]"];
    p_w_picpathView2.layer.cornerRadius = 15;
    p_w_picpathView2.clipsToBounds = YES;
    [view addSubview:p_w_picpathView2];
    [p_w_picpathView2 release];

    UIImageView *p_w_picpathView3 = [[UIImageView alloc] initWithFrame:CGRectMake(280, 0, 30, 30)];
    p_w_picpathView3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    //p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"[email protected]"];
    p_w_picpathView3.layer.cornerRadius = 15;
    p_w_picpathView3.clipsToBounds = YES;
    [view addSubview:p_w_picpathView3];
    [p_w_picpathView3 release];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 37, 200, 2)];
    label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    label.backgroundColor = [UIColor purpleColor];
    [view addSubview:label];
    [label release];
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(60, 0, 160, 36)];
    //autoresizing 自適應高度,是UIView的屬性,可實現自動佈局,
    //   self.textView.autoresizingMask = UIViewAutoresizingNone; //不會隨父視圖的改變而改變
    //   self.textView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; //自動調整view與父視圖左邊距,以保證右邊距不變
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;//自動調整view自身的寬度,保證左邊距和右邊距不變
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;//自動調整view與父視圖右邊距,以保證左邊距不變
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//自動調整view與父視圖上邊距,以保證下邊距不變
    //   self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自動調整view自身的高度,以保證上邊距和下邊距不變
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;//自動調整view與父視圖的下邊距,以保證上邊距不變
    textView.tag = 200;
    textView.scrollEnabled = YES;
    textView.editable = YES;
    textView.textAlignment = NSTextAlignmentLeft;
    textView.dataDetectorTypes = UIDataDetectorTypeAll;
    textView.delegate = self;
    textView.returnKeyType = UIReturnKeyDefault;
    textView.keyboardType = UIKeyboardTypeAlphabet;
    [view addSubview:textView];
    [textView release];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    return YES;
    
}
- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat keyboardHeight = keyboardRect.size.height;
    
   
    /*
    UIView *view = [self.view viewWithTag:100];
    
    CGRect rect = view.frame;// 保存View原有的frame.
    rect.origin.y -= keyboardHeight;//y減去鍵盤的高度
    view.frame = rect;//把最新的frame賦值給view
    [UIView commitAnimations];
    */
    
   self.view.frame = CGRectMake(0, -keyboardHeight, 320, 568);
    
    
}

- (void)keyboardWillHide:(NSNotification *)notification {
    self.view.frame = CGRectMake(0, 0, 320, 568);

    /*
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat keyboardHeight = keyboardRect.size.height;

    UIView *view = [self.view viewWithTag:100];
    
    CGRect rect = view.frame;// 保存View原有的frame.
    rect.origin.y += keyboardHeight;//y減去鍵盤的高度
    view.frame = rect;//把最新的frame賦值給view
    [UIView commitAnimations];
     */
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%@",NSStringFromCGSize(textView.contentSize));
    if ([text isEqual:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    //給輸入框的高度設置限值爲80
    if (textView.contentSize.height >= 80) {
        return;
    }
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; //設置textView的自適應高度,改變自身高度,上下邊框和父視圖的間距保持不變
    UIView *view = (UIView *)[self.view viewWithTag:100];
    CGRect frame = view.frame;
    frame.size.height = textView.contentSize.height;
    frame.origin.y = 568 - textView.contentSize.height;
    view.frame = frame;

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章