iOS中UITextView随键盘上升,随字数增多,TextView增加行数

1、创建一个继承UIView的类

CommentTextView.h

 

@interface CommentTextView : UIView<UITextViewDelegate>

@property(nonatomic,strong) UITextView *textView;

@property (nonatomic,assign) CGFloat keyHight;

 

CommentTextView.m

 

#import "CommentTextView.h"

#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height  //获得屏幕高度

#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width    //获得屏幕宽度

@implementation CommentTextView

{

    float heightText;//文字高度

}

-(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(10, 5, SCREEN_WIDTH-60, frame.size.height-10)];

    self.textView.backgroundColor=[UIColor colorWithRed:233.0/255 green:232.0/255 blue:250.0/255 alpha:1.0];

    self.textView.delegate=self;

    self.textView.returnKeyType=UIReturnKeyNext;

    self.textView.font=[UIFont systemFontOfSize:16];

    [self addSubview:self.textView];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getKeyBoardHeight:) name:@"keyHeight" object:nil];

}

-(void)getKeyBoardHeight:(NSNotification *)notification

{

     NSString *name=[notification.userInfo objectForKey:@"keyHeight"];

    self.keyHight=[name doubleValue];

}

-(void)textViewDidChange:(UITextView *)textView

{

    float currentLineNum=1;//默认文本框显示一行文字

    float textViewWidth=self.textView.frame.size.width;//取得文本框高度

    NSString *content=textView.text;

    NSDictionary *dict=@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]};

    CGSize contentSize=[content sizeWithAttributes:dict];//计算文字长度

    float numLine=ceilf(contentSize.width/textViewWidth); //计算当前文字长度对应的行数

    heightText=contentSize.height;

    NSLog(@"%f,%f",numLine,currentLineNum);

    if(numLine>currentLineNum ){

        //如果发现当前文字长度对应的行数超过。 文本框高度,则先调整当前view的高度和位置,然后调整输入框的高度,最后修改currentLineNum的值.行数大于3行时,不再增加行数

        if(numLine<3)

        {

            self.frame=CGRectMake(self.frame.origin.x,SCREEN_HEIGHT-self.keyHight-50-heightText*(numLine-currentLineNum), self.frame.size.width, 50+heightText*(numLine-currentLineNum));

            textView.frame=CGRectMake(textView.frame.origin.x,5, textView.frame.size.width, 40+heightText*(numLine-currentLineNum));

        }

        else

        {

            self.frame=CGRectMake(self.frame.origin.x,SCREEN_HEIGHT-self.keyHight-50-heightText*2, self.frame.size.width, 50+heightText*2);

            textView.frame=CGRectMake(textView.frame.origin.x,5, textView.frame.size.width, 40+heightText*2);

        }

        currentLineNum=numLine;

    }else if (numLine<currentLineNum ){

        //次数为删除的时候检测文字行数减少的时候

        self.frame=CGRectMake(self.frame.origin.x, SCREEN_HEIGHT-self.keyHight-50, self.frame.size.width, 50);

        textView.frame=CGRectMake(textView.frame.origin.x, 5, textView.frame.size.width, 40);        

        currentLineNum=numLine;

    }

}

在ViewController中引用

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    self.textView=[[CommentTextView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50)];

    self.textView.backgroundColor = [UIColor redColor];

    [self.textView.layer setBorderWidth:0.5];

    [self.textView.layer setBorderColor:[UIColor colorWithRed:188/255.0 green:188/255.0 blue:188/255.0 alpha:1.0].CGColor];

    [self.view addSubview:self.textView];

    

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame=CGRectMake(SCREEN_WIDTH-45, 5, 40, 40);

    btn.titleLabel.textAlignment=NSTextAlignmentCenter;

    [btn setTitle:@"发送" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];

    [self.textView addSubview:btn];

}

-(void)submit

{

    [self.textView.textView resignFirstResponder];

}

-(void)keyboardWillShow:(NSNotification *)notification

{

    CGRect keyBoardRect=[notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat deltaY=keyBoardRect.size.height;

    //获取到键盘高度,并且要返回给CommentText,键盘的高度以计算当前的高度

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

    [dict setValue:[NSString stringWithFormat:@"%f",deltaY] forKey:@"keyHeight"];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"keyHeight" object:nil userInfo:dict];

    [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{

        self.textView.transform=CGAffineTransformMakeTranslation(0, -deltaY);

    }];

}

-(void)keyboardWillHide:(NSNotification *)notification

{

    [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{

               self.textView.transform=CGAffineTransformIdentity;

    } completion:^(BOOL finished) {

    }];

}

运行效果如下

 

posted @ 2015-11-16 14:25  我的style  阅读(644)  评论(1编辑  收藏  举报