输入框适配各种键盘高度

//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];

//获取键盘类型高度

  • (void)keyboardWillShown:(NSNotification*)aNotification{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardHeight = keyboardRect.size.height;
    // _spacingWithKeyboardAndCursor = keyboardHeight - _cursorHeight;
    // if (_spacingWithKeyboardAndCursor > 0) {
    // [UIView animateWithDuration:kAnimationDuration animations:^{
    //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
    self.view.frame = CGRectMake(0.0f, -(keyboardHeight - 65), self.view.frame.size.width, self.view.frame.size.height);
    // }];
    // }

}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification {

        //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
        self.view.frame = CGRectMake(0.0f, 65, self.view.frame.size.width, self.view.frame.size.height);

}

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

if (self.comment == textView) {


    float currentLineNum=1;//默认文本框显示一行文字
    float textViewWidth=self.comment.frame.size.width;//取得文本框高度
    NSString *content=textView.text;
    NSDictionary *dict=@{NSFontAttributeName:[UIFont systemFontOfSize:15.0 * ScreenWidth]};
    CGSize contentSize=[content sizeWithAttributes:dict];//计算文字长度
    float numLine=ceilf(contentSize.width/textViewWidth); //计算当前文字长度对应的行数




    if(numLine > currentLineNum ){
        //如果发现当前文字长度对应的行数超过。 文本框高度,则先调整当前view的高度和位置,然后调整输入框的高度,最后修改currentLineNum的值
        self.backG.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 110 * ScreenHeigth + 15 * ScreenHeigth * (currentLineNum-numLine), [UIScreen mainScreen].bounds.size.width, 45 * ScreenHeigth + 15 * ScreenHeigth *(numLine-currentLineNum));

//
self.comment.frame = CGRectMake(self.comment.frame.origin.x, 5 * ScreenHeigth, self.comment.frame.size.width, 35 * ScreenHeigth + 15 * ScreenHeigth *(numLine-currentLineNum));

         self.view.frame = CGRectMake(0.0f, -(keyboardHeight - 65 ), self.view.frame.size.width, self.view.frame.size.height);

        //textView.frame=CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, 26 + 26*(numLine-currentLineNum));
        currentLineNum=numLine;

        [self.view addSubview:self.backG];

        [self.backG addSubview:self.comment];
        [self.backG addSubview:self.send];


    }else if (numLine - currentLineNum > 0 ){
        //次数为删除的时候检测文字行数减少的时候

        self.backG.frame = CGRectMake(0, self.backG.frame.origin.y - 15 * ScreenHeigth * (currentLineNum-numLine), [UIScreen mainScreen].bounds.size.width, self.backG.frame.size.height - 15 * ScreenHeigth *(numLine-currentLineNum));

        self.comment.frame = CGRectMake(self.comment.frame.origin.x, 5 * ScreenHeigth, self.comment.frame.size.width, self.comment.frame.size.height - 15 * ScreenHeigth * (currentLineNum-numLine));
        //        textView.frame=CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.frame.size.height-20*(currentLineNum-numLine));
        [self.view addSubview:self.backG];

        [self.backG addSubview:self.comment];

        currentLineNum=numLine;
    }


}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值