首页 > 其他 > 详细

弹出键盘textview

时间:2015-04-06 11:32:43      阅读:255      评论:0      收藏:0      [点我收藏+]

- (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 *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 30, 30)];
    imageView1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//设置imageView1的自适应高度,和父视图保持的下边距保持不变
    //imageView1.image = [UIImage imageNamed:@"bottleReceiverVoiceNodePlaying003@2x.png"];
    imageView1.layer.cornerRadius = 15;
    imageView1.clipsToBounds = YES;
    [view addSubview:imageView1];
    [imageView1 release];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 30)];
    imageView2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    //imageView2.image = [UIImage imageNamed:@"Album_ToolViewEmotionHL@2x.png"];
    imageView2.layer.cornerRadius = 15;
    imageView2.clipsToBounds = YES;
    [view addSubview:imageView2];
    [imageView2 release];

    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(280, 0, 30, 30)];
    imageView3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    //imageView3.image = [UIImage imageNamed:@"AlbumAddBtnHL@2x.png"];
    imageView3.layer.cornerRadius = 15;
    imageView3.clipsToBounds = YES;
    [view addSubview:imageView3];
    [imageView3 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;

}

弹出键盘textview

原文:http://9692896.blog.51cto.com/9682896/1629193

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!