弹出键盘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 *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 30, 30)];
    imageView1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//设置imageView1的自适应高度,和父视图保持的下边距保持不变
    //imageView1.image = [UIImage imageNamed:@"[email protected]"];
    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:@"[email protected]"];
    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:@"[email protected]"];
    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;

}

时间: 2024-11-07 13:47:56

弹出键盘textview的相关文章

Android弹出键盘布局闪动原理和解决

弹出键盘布局闪动原理和解决 在开发中,遇到一个问题:做一个微信一样,表情输入和软键盘在切换的时候,聊天界面不闪动的问题.为了解决这个问题,需要知道一下Android的软键盘弹出的时候发生的几个变化. 当AndroidMainfest.xml 中配置android:windowSoftInputMode="adjustResize|stateHidden" 属性后,如果弹出软键盘,那么会重绘界面.基本流程如下(API 10): 1.  Android 收到打开软键盘命令 2.  Andr

弹出键盘怎样把tableview往上顶?不遮住输入行

弹出键盘怎样把tableview往上顶?不遮住输入行            - (void)registerForKeyboardNotifications {    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];    [[NSNotificationCenter

UI弹出键盘和收回键盘

点击textfield,会自动弹出键盘 要让键盘收回来,先设置个代理:[field setTextFieldDelegate:self];  可设置成自己,也可设置成其他对象,只要在对应的类中,遵循UITextFieldDelegate协议 在UITextFieldDelegate协议中,有一些可选的方法: //点击return回收键盘 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstRe

关于android中EditText自动获取焦点并弹出键盘的相关设置

在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法: 需求:EditText自动获取焦点并弹出键盘,代码: EditText.setFocusable(true); EditText.setFocusableInTouchMode(true); EditText.requestFocus(); 需求:EditText不会自动获取焦点并且不会弹出键盘,代码:  将其父控件设置: P

IPhone手机页面中点击文本输入框,弹出键盘,网页会放大,如何解决

在head标签中加入以上meta声明.具体属性可以谷歌/百度. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 我查了下viewport,有几个属性:width - viewport的宽度 height - viewport的高度initial-scale - 初始的缩放比例minim

获取弹出键盘时间,,,与高度

弹出键盘操作 //1.定制通知 //在初始化时定制通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //2.对应的方法. -(void)KeyboardWillShow:(NSNotification *)notification { NSDictionary

让UIWebView弹出键盘上的按钮显示中文

UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的,有时我们想把按钮文字变为中文 其实办法很简单,只需要让你的应用程序支持中文本地化,意思是在项目中新建一个中文的本地化文件夹zh-Hans.lproj 如果还不太了解什么叫本地化,可以看看我的这篇文章<应用程序本地化> 下面简单演示下操作步骤: 1.添加中文本地化支持 2.选择要支持本地化的文件,至

弹出键盘时,让table向上移动

弹出键盘时,让table向上移动 #pragma mark UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing: (UITextField *)textField { [UIView beginAnimations: nil context: nil]; self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 280, 0); NSIndexPath *indexPath = [NSIn

IOS开发之自定义系统弹出键盘上方的view

IOS开发之自定义系统弹出键盘上方的view 分类: IOS 2014-11-18 09:26 1304人阅读 评论(0) 收藏 举报 目录(?)[+] 这篇文章解决的一个开发中的实际问题就是:当弹出键盘时,自定义键盘上方的view.目前就我的经验来看,有两种解决方法.一个就是利用 UITextField或者UITextView的inputAccessoryView属性,另一种,就是监听键盘弹出的notification来自 己解决相关视图的位置问题. 第一种解决方法相对比较简单,第二种的方法中