UItextView的用法及技巧(iOS7下光标bug解决方案)

一、新建一个textView
01 //初始化
02 UITextView *textView = [[[UITextView alloc] init] autorelease];
03  
04 //设置代理 需在interface中声明UITextViewDelegate
05 textView.delegate = self;
06  
07 //字体大小
08 textView.font = [UIFont systemFontOfSize:16];
09  
10 //添加滚动区域
11 textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0);
12  
13 //是否可以滚动
14 textView.scrollEnabled = NO;
15  
16 //获得焦点
17 [textView becomeFirstResponder];
1 [self.view addSubview:textView];
二、键盘操作
1 //返回键的类型
2 textView.returnKeyType = UIReturnKeyDefault;
3  
4 //键盘类型
5 textView.keyboardType = UIKeyboardTypeDefault;
三、隐藏键盘的几种方式

个人还是认为最方便的是在键盘上加上一个ToolBar,在上面加上一个按钮来隐藏键盘

①在键盘上加上隐藏按钮

01 //定义一个toolBar
02 UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
03  
04 //设置style
05 [topView setBarStyle:UIBarStyleBlack];
06  
07 //定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
08 UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
09  
10 UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
11  
12 //定义完成按钮
13 UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone  target:self action:@selector(resignKeyboard)];
14      
15 //在toolBar上加上这些按钮
16 NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];    
17 [topView setItems:buttonsArray];
18  
19 [textView setInputAccessoryView:topView];
1 //隐藏键盘
2 - (void)resignKeyboard {
3     [textView resignFirstResponder];
4 }
最终效果

还有几种也可隐藏键盘的方式

②用回车键,前提是你的textView中不需要用到回车键

1 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
2  if ([text isEqualToString:@"\n"])
3 {
4 [textView resignFirstResponder]; return NO;
5 }
6  return YES;
7 }

③触摸空白处隐藏键盘

1 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
2 {
3     //隐藏键盘
4     [textView resignFirstResponder];
5 }
四、使键盘不挡住输入框

    在view中添加一个子view,设置此子view的tag值为1000,在此view上添加一个textView和一个发送按钮,如下图;我们要达到textView的键盘弹出时,整个View往上平移,键盘消失,view往下平移的效果,模拟发送短信的界面。

设置textView圆角

1 //设置textView圆角
2 [self.textView.layer setCornerRadius:10];

①、在viewWillAppear中添加键盘监听事件

1 //添加键盘的监听事件
2      
3     //注册通知,监听键盘弹出事件
4     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
5      
6     //注册通知,监听键盘消失事件
7     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];
②、完成①selector中键盘弹出keyboardDidShow:和消失keyboardDidHidden方法

    在.m文件#import后面添加

1 //动画时间
2 #define kAnimationDuration 0.2
3 //view高度
4 #define kViewHeight 56
键盘出现
01 // 键盘弹出时
02 -(void)keyboardDidShow:(NSNotification *)notification
03 {
04      
05     //获取键盘高度
06     NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
07      
08     CGRect keyboardRect;
09      
10     [keyboardObject getValue:&keyboardRect];
11      
12     //调整放置有textView的view的位置
13      
14        //设置动画
15     [UIView beginAnimations:nil context:nil];
16      
17        //定义动画时间
18     [UIView setAnimationDuration:kAnimationDuration];
19      
20        //设置view的frame,往上平移
21     [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)];
22      
23     [UIView commitAnimations];
24      
25 }
键盘消失
01 //键盘消失时
02 -(void)keyboardDidHidden
03 {
04     //定义动画
05     [UIView beginAnimations:nil context:nil];
06     [UIView setAnimationDuration:kAnimationDuration];
07     //设置view的frame,往下平移
08     [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)];
09     [UIView commitAnimations];
10 }
效果图:

iOS7光标问题

PS:有网友遇到textView在ios7上出现编辑进入最后一行时光标消失,看不到最后一行,变成盲打,stackOverFlow网站上有大神指出,是ios7本身bug,加上下面一段代码即可(网友调试得出,在此mark一下,有问题,欢迎大神们指出)

01 -(void)textViewDidChange:(UITextView *)textView {
02     CGRect line = [textView caretRectForPosition:
03                    textView.selectedTextRange.start];
04     CGFloat overflow = line.origin.y + line.size.height
05     - ( textView.contentOffset.y + textView.bounds.size.height
06        - textView.contentInset.bottom - textView.contentInset.top );
07     if ( overflow > 0 ) {
08         // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
09         // Scroll caret to visible area
10         CGPoint offset = textView.contentOffset;
11         offset.y += overflow + 7; // leave 7 pixels margin
12         // Cannot animate with setContentOffset:animated: or caret will not appear
13         [UIView animateWithDuration:.2 animations:^{
14             [textView setContentOffset:offset];
15         }];
16     }
17 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值