天天看點

iOS UITextView 高度随文字自動增加,并跟随鍵盤移動(一)

項目中遇到這樣一個需求 ,有個文本框,需要随着使用者輸入的文字多少高度自動增加。

比如說,當使用者輸入的文字不足一行的時候textview的高度為初始高度,

當輸入的文字超過一行,不足兩行的時候,我們将textView 的高度調整為顯示兩行文字的高度。

此處,我們要實作一個評論的功能,還需要輸入框跟随鍵盤移動。

開始代碼

首先,我們建立一個類,專門管理輸入框,我們起名:CommentView 繼承 UIView

為他建立一個UITextView (我們的輸入框)

#import <UIKit/UIKit.h>

@interface CommentView : UIView

@property(nonatomic,strong) UITextView *textView;

-(void)inittextFrame;

@end
           

接下來我們在實作類中 初始化輸入框

//擷取螢幕 寬度、高度
#define SCREEN_FRAME ([UIScreen mainScreen].bounds)
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


#import "CommentView.h"

@implementation CommentView


-(id)initWithFrame:(CGRect)frame
{
    
    self=[super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor blueColor];
        [self initTextView:frame];
    }
    return self;
}
-(void)initTextView:(CGRect)frame
{
    
    
    self.textView=[[UITextView alloc]initWithFrame:CGRectMake(2, 2, SCREEN_WIDTH-2*2, frame.size.height-2*2)];
    
    
    
    self.textView.backgroundColor=[UIColor colorWithRed:233.0/255 green:232.0/255 blue:250.0/255 alpha:1.0];
    [self addSubview:self.textView];
    
    
}


@end
           

我們先添加一下 CommentView  看出來的效果

我們添加一個按鈕,當點選按鈕的時候 打開ComentView

//擷取螢幕 寬度、高度
#define SCREEN_FRAME ([UIScreen mainScreen].bounds)
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


#import "ViewController.h"
#import "CommentView.h"

@interface ViewController ()
{
    
    CommentView *commentV;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIButton *testBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 120, 88, 36)];
    [testBtn setTitle:@"testBtn" forState:UIControlStateNormal];
    [testBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [testBtn addTarget:self action:@selector(OpentestView) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:testBtn];
}

-(void)OpentestView
{
    
    commentV=[[CommentView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-40, SCREEN_WIDTH, 40)];
    [self.view addSubview:commentV];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
           

運作,點選button 彈出CommentView 效果如下

iOS UITextView 高度随文字自動增加,并跟随鍵盤移動(一)

接下來 我們讓文本框跟随鍵盤

我們在打開ContentView 打開鍵盤

然後添加鍵盤打開和關閉的通知,監聽到通知後,調整CommentView的位置

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    
           
#pragma mark keyboardNotification
-(void)keyboardShow:(NSNotification *)note
{
    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat deltaY=keyBoardRect.size.height;
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        commentV.transform=CGAffineTransformMakeTranslation(0, -deltaY);
    }];
}
-(void)keyboardHide:(NSNotification *)note
{
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        commentV.transform=CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        commentV.textView.text=@"";
        [commentV removeFromSuperview];
    }];
}
           

我們測試一下 效果

iOS UITextView 高度随文字自動增加,并跟随鍵盤移動(一)

好了到目前為止,鍵盤調用,文本框跟随鍵盤基本實作。

下一節我們來實作文本框自動調節高度。

下節位址 http://blog.csdn.net/lwjok2007/article/details/47403511

代碼上傳至群空間 【文本框高度自動調整1.zip】

蘋果開發群 :414319235  歡迎加入  歡迎讨論問題