textView的学习使用

来源:互联网 发布:如何设置php头部 编辑:程序博客网 时间:2024/06/14 12:17

//初始化并定义大小   

 UITextView *textview = [[UITextView allocinitWithFrame:CGRectMake(201028030)];

    textview.backgroundColor=[UIColor whiteColor]; //背景色

    textview.scrollEnabled = NO   //当文字超过视图的边框时是否允许滑动,默认为“YES”

    textview.editable = YES       //是否允许编辑内容,默认为“YES”

    textview.delegate = self;       //设置代理方法的实现类

    textview.font=[UIFont fontWithName:@"Arial" size:18.0]; //设置字体名字和字体大小;

    textview.returnKeyType = UIReturnKeyDefault;//return键的类型

    textview.keyboardType = UIKeyboardTypeDefault;//键盘类型

    textview.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左

    textview.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式(如电话号码、网址、地址等)

    textview.textColor = [UIColor blackColor];

    textview.text = @"UITextView详解";//设置显示的文本内容

    [self.view addSubview:textview];

//添加滚动区域
textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0);

//获得焦点
[textView becomeFirstResponder];

UITextView的代理方法如下:

//将要开始编辑

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;


//将要结束编辑

- (BOOL)textViewShouldEndEditing:(UITextView *)textView;


//开始编辑

- (void)textViewDidBeginEditing:(UITextView *)textView;


//结束编辑

- (void)textViewDidEndEditing:(UITextView *)textView;


//内容将要发生改变编辑

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;


//内容发生改变编辑

- (void)textViewDidChange:(UITextView *)textView;


//焦点发生改变

- (void)textViewDidChangeSelection:(UITextView *)textView;


隐藏键盘的几种方式  个人还是认为最方便的是在键盘上加上一个ToolBar,在上面加上一个按钮来隐藏键盘 
①在键盘上加上隐藏按钮
  1. //定义一个toolBar
  2. UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];

  3. //设置style
  4. [topView setBarStyle:UIBarStyleBlack];

  5. //定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
  6. UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];

  7. UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];

  8. //定义完成按钮
  9. UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone  target:self action:@selector(resignKeyboard)];
  10.     
  11. //在toolBar上加上这些按钮
  12. NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];     
  13. [topView setItems:buttonsArray];

  14. [textView setInputAccessoryView:topView];
复制代码
最终效果 

131735_hlRQ_735123.png



还有几种也可隐藏键盘的方式 
②用回车键,前提是你的textView中不需要用到回车键
  1. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  2. if ([text isEqualToString:@""]) 

  3. [textView resignFirstResponder]; return NO; 
  4. }
  5. return YES; 
  6. }

③触摸空白处隐藏键盘
  1. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  2. {
  3.     //隐藏键盘
  4.     [textView resignFirstResponder];
  5. }

四、使键盘不挡住输入框      在view中添加一个子view,设置此子view的tag值为1000,在此view上添加一个textView和一个发送按钮,如下图;我们要达到textView的键盘弹出时,整个View往上平移,键盘消失,view往下平移的效果,模拟发送短信的界面。 
100937_IaJo_735123.png 
设置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

键盘出现
  1. // 键盘弹出时
  2. -(void)keyboardDidShow:(NSNotification *)notification
  3. {
  4.     //获取键盘高度
  5.     NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
  6.     CGRect keyboardRect;
  7.     [keyboardObject getValue:&keyboardRect];
  8.     //调整放置有textView的view的位置
  9.     
  10.        //设置动画
  11.     [UIView beginAnimations:nil context:nil];
  12.     
  13.        //定义动画时间
  14.     [UIView setAnimationDuration:kAnimationDuration];
  15.     
  16.        //设置view的frame,往上平移
  17.     [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)];
  18.     
  19.     [UIView commitAnimations];
  20.     
  21. }
复制代码
键盘消失
  1. //键盘消失时
  2. -(void)keyboardDidHidden
  3. {
  4.     //定义动画
  5.     [UIView beginAnimations:nil context:nil];
  6.     [UIView setAnimationDuration:kAnimationDuration];
  7.     //设置view的frame,往下平移
  8.     [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)];
  9.     [UIView commitAnimations];
  10. }
复制代码
效果图:


2013-8-20 16:12:54 上传
下载附件 (24.18 KB)

104556_cpSI_735123.png

0 0
原创粉丝点击