iOS UITextField最大字符数和字节数的限制详解

2020-01-18 18:16:56于丽

1、真机

如果使用了中文输入法,注册的keyboardWillShow会回调两次。第一次是键盘默认高度216,第二次则是加了keyboard的导航栏的高度。

2、模拟器

第一次弹出键盘没有问题

uitextfield,最大长度,uitextfield输入限制,uitextfield限制字数

打印userinfo:


(lldb) po userInfo
{
 UIKeyboardAnimationCurveUserInfoKey = 7;
 UIKeyboardAnimationDurationUserInfoKey = "0.25";
 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 226}}";
 UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 849}";
 UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 623}";
 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 226}}";
 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 510}, {414, 226}}";
 UIKeyboardIsLocalUserInfoKey = 1;
}

此时我们去按123旁边的小圆球会出现如下的图:

uitextfield,最大长度,uitextfield输入限制,uitextfield限制字数

打印userinfo:


(lldb) po userInfo
{
 UIKeyboardAnimationCurveUserInfoKey = 7;
 UIKeyboardAnimationDurationUserInfoKey = "0.25";
 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";
 UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 623}";
 UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";
 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 510}, {414, 226}}";
 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";
 UIKeyboardIsLocalUserInfoKey = 1;
}

键盘被遮挡了。

总结:观察结果,发现了这个规律,打印一下时间,还有一个问题就是,中文键盘第一次启动的时候会回调两次。


keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

所以去掉这句话即可

六. 使用封装的XXTextField

UITextView , UITextfield 中如果有keyboard的时候,需要一个自动弹起事件,以及弹起之后的content的偏移对父view的处理。如果每个页面都实现一次会非常复杂。这里我们介绍一种自动化的处理机制。在此之前,先介绍一下文字处理框架.最后给大家推荐一下我写的 XXTextField ,大家也可以在此基础上自己添加一些正则表达式。

1.解决uiview中的textfield 遮挡问题


_textfieldName.keyboardType = UIKeyboardTypeDefault;
_textfieldName.inputType = XXTextFieldTypeOnlyInt;
_textfieldName.maxLength = 5;
_textfieldPwd.inputType = XXTextFieldTypeForbidEmoj;

#import "XXKeyboardManager.h"
@interface XXCorrectVC ()<XXKeyboardManagerShowHiddenNotificationDelegate>
@end

@implementation XXCorrectVC

- (void)viewDidLoad {
 [super viewDidLoad];
 [[XXKeyboardManager sharedInstance] setDelegate:self];
 // Do any additional setup after loading the view from its nib.
}
#pragma mark- KeyBoardShow/Hidden
- (void)showKeyboardWithRect:(CGRect)keyboardRect
    withDuration:(CGFloat)animationDuration
{
 CGFloat offset = self.textFieldCorrect.frame.size.height + self.textFieldCorrect.frame.origin.y - keyboardRect.origin.y;
 if(offset < 0){
  return;
 }
 [UIView animateWithDuration:animationDuration
       delay:0.f
      options:UIViewAnimationOptionCurveEaseInOut animations:^{
  CGRect rect = CGRectMake(0.0f, -offset,self.view.frame.size.width,self.view.frame.size.height);
  self.view.frame = rect;
 } completion:^(BOOL finished) {
  
 }];
}

- (void)hiddenKeyboardWithRect:(CGRect)keyboardRect
     withDuration:(CGFloat)animationDuration
{
 [UIView animateWithDuration:animationDuration
       delay:0.f
      options:UIViewAnimationOptionCurveEaseInOut animations:^{
  self.textFieldCorrect.frame = self.view.bounds;
 } completion:^(BOOL finished) {
 }];
}
@end