iOS UITextView 高度随文字自动增加,并跟随键盘移动(一)

 

iOS UITextView 高度随文字自动增加,并跟随键盘移动(一)

标签: iostextview跟随键盘自适应
  961人阅读  评论(0)  收藏  举报
  分类:

项目中遇到这样一个需求 ,有个文本框,需要随着用户输入的文字多少高度自动增加。

比如说,当用户输入的文字不足一行的时候textview的高度为初始高度,

当输入的文字超过一行,不足两行的时候,我们将textView 的高度调整为显示两行文字的高度。


此处,我们要实现一个评论的功能,还需要输入框跟随键盘移动。


开始代码

首先,我们新建一个类,专门管理输入框,我们起名:CommentView 继承 UIView

为他创建一个UITextView (我们的输入框)


[objc]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface CommentView : UIView  
  4.   
  5. @property(nonatomic,strongUITextView *textView;  
  6.   
  7. -(void)inittextFrame;  
  8.   
  9. @end  



接下来我们在实现类中 初始化输入框


[objc]  view plain copy
  1. //获取屏幕 宽度、高度  
  2. #define SCREEN_FRAME ([UIScreen mainScreen].bounds)  
  3. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)  
  4. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)  
  5.   
  6.   
  7. #import "CommentView.h"  
  8.   
  9. @implementation CommentView  
  10.   
  11.   
  12. -(id)initWithFrame:(CGRect)frame  
  13. {  
  14.       
  15.     self=[super initWithFrame:frame];  
  16.     if (self) {  
  17.         self.backgroundColor=[UIColor blueColor];  
  18.         [self initTextView:frame];  
  19.     }  
  20.     return self;  
  21. }  
  22. -(void)initTextView:(CGRect)frame  
  23. {  
  24.       
  25.       
  26.     self.textView=[[UITextView alloc]initWithFrame:CGRectMake(22, SCREEN_WIDTH-2*2, frame.size.height-2*2)];  
  27.       
  28.       
  29.       
  30.     self.textView.backgroundColor=[UIColor colorWithRed:233.0/255 green:232.0/255 blue:250.0/255 alpha:1.0];  
  31.     [self addSubview:self.textView];  
  32.       
  33.       
  34. }  
  35.   
  36.   
  37. @end  



我们先添加一下 CommentView  看出来的效果

我们添加一个按钮,当点击按钮的时候 打开ComentView

[objc]  view plain copy
  1. //获取屏幕 宽度、高度  
  2. #define SCREEN_FRAME ([UIScreen mainScreen].bounds)  
  3. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)  
  4. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)  
  5.   
  6.   
  7. #import "ViewController.h"  
  8. #import "CommentView.h"  
  9.   
  10. @interface ViewController ()  
  11. {  
  12.       
  13.     CommentView *commentV;  
  14. }  
  15. @end  
  16.   
  17. @implementation ViewController  
  18.   
  19. - (void)viewDidLoad {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view, typically from a nib.  
  22.       
  23.     UIButton *testBtn=[[UIButton alloc]initWithFrame:CGRectMake(101208836)];  
  24.     [testBtn setTitle:@"testBtn" forState:UIControlStateNormal];  
  25.     [testBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];  
  26.     [testBtn addTarget:self action:@selector(OpentestView) forControlEvents:UIControlEventTouchDown];  
  27.     [self.view addSubview:testBtn];  
  28. }  
  29.   
  30. -(void)OpentestView  
  31. {  
  32.       
  33.     commentV=[[CommentView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-40, SCREEN_WIDTH, 40)];  
  34.     [self.view addSubview:commentV];  
  35.       
  36. }  
  37.   
  38. - (void)didReceiveMemoryWarning {  
  39.     [super didReceiveMemoryWarning];  
  40.     // Dispose of any resources that can be recreated.  
  41. }  
  42.   
  43. @end  

运行,点击button 弹出CommentView 效果如下



接下来 我们让文本框跟随键盘

我们在打开ContentView 打开键盘


然后添加键盘打开和关闭的通知,监听到通知后,调整CommentView的位置


[objc]  view plain copy
  1. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];  
  2. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];  

[objc]  view plain copy
  1. #pragma mark keyboardNotification  
  2. -(void)keyboardShow:(NSNotification *)note  
  3. {  
  4.     CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];  
  5.     CGFloat deltaY=keyBoardRect.size.height;  
  6.     [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{  
  7.         commentV.transform=CGAffineTransformMakeTranslation(0, -deltaY);  
  8.     }];  
  9. }  
  10. -(void)keyboardHide:(NSNotification *)note  
  11. {  
  12.     [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{  
  13.         commentV.transform=CGAffineTransformIdentity;  
  14.     } completion:^(BOOL finished) {  
  15.         commentV.textView.text=@"";  
  16.         [commentV removeFromSuperview];  
  17.     }];  
  18. }  



我们测试一下 效果




好了到目前为止,键盘调用,文本框跟随键盘基本实现。

下一节我们来实现文本框自动调节高度。

下节地址 http://blog.csdn.net/lwjok2007/article/details/47403511


代码上传至群空间 【文本框高度自动调整1.zip

苹果开发群 :414319235  欢迎加入  欢迎讨论问题


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值