iOS键盘处理
#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];
}];
}