UITextView的用法及技巧
2013-09-30 17:23
239 查看
摘要:textView的创建;设置属性;代理方法;隐藏键盘;键盘弹出时调整textView的位置,像手机短信的效果
个人还是认为最方便的是在键盘上加上一个ToolBar,在上面加上一个按钮来隐藏键盘
①在键盘上加上隐藏按钮
最终效果
还有几种也可隐藏键盘的方式
②用回车键,前提是你的textView中不需要用到回车键
③触摸空白处隐藏键盘
在view中添加一个子view,设置此子view的tag值为1000,在此view上添加一个textView和一个发送按钮,如下图;我们要达到textView的键盘弹出时,整个View往上平移,键盘消失,view往下平移的效果,模拟发送短信的界面。
设置textView圆角
①、在viewWillAppear中添加键盘监听事件
②、完成①selector中键盘弹出keyboardDidShow:和消失keyboardDidHidden方法
在.m文件#import后面添加
键盘出现
键盘消失
原文地址:http://my.oschina.net/joanfen/blog/140143
一、新建一个textView
01 | //初始化 |
02 | UITextView *textView=[[[UITextViewalloc]init]autorelease]; |
03 |
04 | //设置代理 需在interface中声明UITextViewDelegate |
05 | textView.delegate =self; |
06 |
07 | //字体大小 |
08 | textView.font =[UIFontsystemFontOfSize: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=[[UIToolbaralloc]initWithFrame:CGRectMake(0,0,320,30)]; |
03 |
04 | //设置style |
05 | [topView setBarStyle:UIBarStyleBlack]; |
06 |
07 | //定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边 |
08 | UIBarButtonItem *button1=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil]; |
09 |
10 | UIBarButtonItem *button2=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil]; |
11 |
12 | //定义完成按钮 |
13 | UIBarButtonItem *doneButton=[[UIBarButtonItemalloc]initWithTitle:@ "完成" style:UIBarButtonItemStyleDone target:selfaction:@selector(resignKeyboard)]; |
14 |
15 | //在toolBar上加上这些按钮 |
16 | NSArray |
17 | [topView setItems:buttonsArray]; |
18 |
19 | [textView setInputAccessoryView:topView]; |
1 | //隐藏键盘 |
2 | - ( void )resignKeyboard { |
3 | [textView resignFirstResponder]; |
4 | } |
还有几种也可隐藏键盘的方式
②用回车键,前提是你的textView中不需要用到回车键
1 | -( BOOL )textView:(UITextView *)textViewshouldChangeTextInRange:(NSRange)rangereplacementText:(NSString*)text{ |
2 | if ([text isEqualToString:@ "\n" ]) |
3 | { |
4 | [textView resignFirstResponder]; return NO; |
5 | } |
6 | return YES; |
7 | } |
1 | -( void )touchesBegan:(NSSet |
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]; |
1 | //添加键盘的监听事件 |
2 |
3 | //注册通知,监听键盘弹出事件 |
4 | [[NSNotificationCenter |
5 |
6 | //注册通知,监听键盘消失事件 |
7 | [[NSNotificationCenter |
在.m文件#import后面添加
1 | //动画时间 |
2 | #define |
3 | //view高度 |
4 | #define |
01 | // 键盘弹出时 |
02 | -( void )keyboardDidShow:(NSNotification *)notification |
03 | { |
04 |
05 | //获取键盘高度 |
06 | NSValue |
07 |
08 | CGRect keyboardRect; |
09 |
10 | [keyboardObject getValue:&keyboardRect]; |
11 |
12 | //调整放置有textView的view的位置 |
13 |
14 | //设置动画 |
15 | [UIView |
16 |
17 | //定义动画时间 |
18 | [UIView setAnimationDuration:kAnimationDuration]; |
19 |
20 | //设置view的frame,往上平移 |
21 | [(UIView |
22 |
23 | [UIView commitAnimations]; |
24 |
25 | } |
01 | //键盘消失时 |
02 | -( void )keyboardDidHidden |
03 | { |
04 | //定义动画 |
05 | [UIView |
06 | [UIView setAnimationDuration:kAnimationDuration]; |
07 | //设置view的frame,往下平移 |
08 | [(UIView |
09 | [UIView commitAnimations]; |
10 | } |
效果图:
原文地址:http://my.oschina.net/joanfen/blog/140143
相关文章推荐
- UItextView的用法及技巧(iOS7下光标bug解决方案)
- UITextView的用法及技巧
- iOS学习4:UITextView的用法及技巧(ios7下光标bug解决方案)
- UITextView的用法及技巧
- UITextView的用法及技巧(ios7下光标bug解决方案)
- iOS学习4:UITextView的用法及技巧(ios7下光标bug解决方案)
- iOS学习4:UITextView的用法及技巧(ios7下光标bug解决方案)
- UITextView的用法及技巧(ios7下光标bug解决方案)
- iOS学习4:UITextView的用法及技巧(ios7下光标bug解决方案)
- iOS开发-UITextView的用法及技巧(ios7下光标bug解决方案)
- winform技巧—DataGridView控件用法
- Eclipse用法和技巧二十四:当git遇上eclipse
- Eclipse用法和技巧十二:快速复制一行
- Eclipse用法和技巧二十五:eclipse图标的含义
- Eclipse用法和技巧之自动添加try/catch异常块
- Java中Split函数的用法技巧
- Eclipse用法和技巧二:自动生成Main方法1
- Eclipse用法和技巧四:生成说明文档1
- Eclipse用法和技巧十四:自动生成的TODO注释2
- Eclipse用法和技巧二十五:eclipse图标的含义