码迷,mamicode.com
首页 > 移动开发 > 详细

iOS键盘处理

时间:2021-07-01 17:00:12      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:err   res   oar   dshow   constrain   sel   bool   hpa   nss   

#pragma mark - TextViewDelegate

 

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

    if ([text isEqualToString:@"\n"]) {

        [textView resignFirstResponder];

        return NO;

    }

    if ([text isEqualToString:@""]) {

        return YES;

    }

    NSString *tem = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];

   if (![text isEqualToString:tem]) {

      return NO;

   }

}

 

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

    //屏蔽emoji表情

    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:0 error:nil];

 

    NSString *noEmojiStr = [regularExpression stringByReplacingMatchesInString:textView.text options:0 range:NSMakeRange(0, textView.text.length) withTemplate:@""];

 

    if (![noEmojiStr isEqualToString:textView.text]){

        textView.text = noEmojiStr;

    }

    

    if (textView.text.length > 30) {

     UITextRange *markedRange = [textView markedTextRange];

      if (markedRange) {

         return;

      }

       NSRange range = [textView.text rangeOfComposedCharacterSequenceAtIndex:30];

       textView.text = [textView.text substringToIndex:range.location];

    }

    self.confirmButton.enabled = textView.text.length;

    self.editCountLabel.text = [NSString stringWithFormat:@"%ld/30",textView.text.length];

}

 

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

}

    

#pragma mark - keyBoardShow/KeyBoardHide

 

- (CGFloat)targetViewBottomInKeyWindow {

    UIView *targetView = self.editTextView;

    UIWindow* desWindow = [UIApplication sharedApplication].keyWindow;

    CGRect frame = [targetView convertRect:targetView.bounds toView:desWindow];

    CGFloat bottom = kScreenHeight - frame.origin.y - frame.size.height;

    return bottom;

}

 

- (void)keyboardWasShown:(NSNotification *)notification {

    CGFloat targetViewBottom = [self targetViewBottomInKeyWindow];

    NSDictionary *info = [notification userInfo];

    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGSize keyboardSize = [value CGRectValue].size;

    CGFloat keyboardHeight = keyboardSize.height;

    if (targetViewBottom >= keyboardHeight) {

        return;

    }

    [self.view setNeedsUpdateConstraints];

    [UIView animateWithDuration:duration animations:^{

        [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.left.right.equalTo(@0);

            make.height.equalTo(@(kScreenHeight));

            make.bottom.equalTo(@(-(keyboardHeight-targetViewBottom)+0-15));

        }];

        [self.view layoutIfNeeded];

    }];

}

 

- (void)keyboardWasHidden:(NSNotification *)notification {

    NSDictionary *info = [notification userInfo];

    NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [self.view setNeedsUpdateConstraints];

    [UIView animateWithDuration:duration animations:^{

        [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.bottom.left.right.equalTo(@0);

            make.height.equalTo(@(kScreenHeight));

        }];

        [self.view layoutIfNeeded];

    }];

}

iOS键盘处理

标签:err   res   oar   dshow   constrain   sel   bool   hpa   nss   

原文地址:https://www.cnblogs.com/syh918/p/14957572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!