// // SPSBKeyboardManager.m // 我的社保 // // Created by shanp on 2021/4/25. // #import "SPSBKeyboardManager.h" #import #import #import #import "SPSBTextField.h" #import #import "SPSBGeneralManager.h" @interface SPSBKeyboardManager () { UIToolbar *_toolbar; SPSBTextField *_textField; } @end @implementation SPSBKeyboardManager + (instancetype)alloc { NSAssert(![self isMemberOfClass:[SPSBKeyboardManager class]], @"SPSBKeyboardManager is singleton, you should not instantiate it directly."); return [super alloc]; } + (SPSBKeyboardManager *)shareManager { static SPSBKeyboardManager *manager = nil; static dispatch_once_t once; dispatch_once(&once, ^{ manager = [[super allocWithZone:nil] init]; [manager addKeyboardTool]; }); return manager; } + (instancetype)allocWithZone:(struct _NSZone *)zone { return [self shareManager]; } - (void)addKeyboardTool { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UITextFieldTextDidBeginEditingNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UITextViewTextDidBeginEditingNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil]; _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, jxh_screenHeight(), jxh_screenWidth(), 44)]; _toolbar.items = @[[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(HiddenKeyBoard)]]; _textField = [[SPSBTextField alloc] initWithFrame:CGRectMake(15, 0, jxh_screenWidth() * 2 / 3, 44)]; _textField.enabled = false; [_toolbar addSubview:_textField]; } - (void)removeKeyboardTool { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil]; _toolbar = nil; _textField = nil; } - (void)keyboardWillShow:(NSNotification *)sender { UIViewController *vc = jxh_findPresentedViewControllerStartingFrom(spsb_keyWindow().rootViewController); if (![NSStringFromClass([vc class]) hasPrefix:@"SPSB"]) return; debugLog(@"-----------%@", sender.userInfo); id firstResponder; for (UIWindow *window in jxh_application().windows) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" SEL f = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"first", @"Responder"]); firstResponder = [window performSelector:f]; #pragma clang diagnostic pop if (firstResponder) { break; } } if ([firstResponder isKindOfClass:[UITextField class]]) { UITextField *_firstResponder = firstResponder; objc_setAssociatedObject(_firstResponder, &kKeyboardKey, _textField, OBJC_ASSOCIATION_RETAIN_NONATOMIC); _textField.placeholder = _firstResponder.placeholder; _textField.text = _firstResponder.text; _textField.secureTextEntry = _firstResponder.secureTextEntry; if ([firstResponder isKindOfClass:[SPSBTextField class]]) { _textField.spsb_maxCount = ((SPSBTextField *)_firstResponder).spsb_maxCount; } else { _textField.spsb_maxCount = 0; } if (_firstResponder.inputAccessoryView) { _firstResponder.inputAccessoryView = nil; } _firstResponder.inputAccessoryView = _toolbar; [_firstResponder reloadInputViews]; } else if ([firstResponder isKindOfClass:[UITextView class]]) { UITextView *_firstResponder = firstResponder; _textField.placeholder = @""; _textField.text = @""; if (_firstResponder.inputAccessoryView) { _firstResponder.inputAccessoryView = nil; } _firstResponder.inputAccessoryView = _toolbar; [_firstResponder reloadInputViews]; } } static char kKeyboardKey; - (void)keyboardWillChangeFrame:(NSNotification *)sender { UIViewController *vc = jxh_findPresentedViewControllerStartingFrom(spsb_keyWindow().rootViewController); if (![NSStringFromClass([vc class]) hasPrefix:@"SPSB"]) return; debugLog(@"======================%@", sender.userInfo); id firstResponder; for (UIWindow *window in jxh_application().windows) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" SEL f = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"first", @"Responder"]); firstResponder = [window performSelector:f]; #pragma clang diagnostic pop if (firstResponder) { break; } } if ([firstResponder isKindOfClass:[UITextField class]]) { UITextField *_firstResponder = firstResponder; debugLog(@"%@", _firstResponder); CGFloat keyboardY = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; if (keyboardY == jxh_screenHeight()) { _firstResponder.inputAccessoryView = nil; } } else if ([firstResponder isKindOfClass:[UITextView class]]) { UITextView *_firstResponder = firstResponder; debugLog(@"%@", _firstResponder); CGFloat keyboardY = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; if (keyboardY == jxh_screenHeight()) { _firstResponder.inputAccessoryView = nil; } } } - (void)textFieldDidChange:(NSNotification *)sender { debugLog(@"_____________%@", sender); UITextField *textField = sender.object; SPSBTextField *textField1 = objc_getAssociatedObject(textField, &kKeyboardKey); textField1.text = textField.text; [textField1 textDidChange]; } - (void)HiddenKeyBoard { UIViewController *vc = jxh_findPresentedViewControllerStartingFrom(spsb_keyWindow().rootViewController); if (![NSStringFromClass([vc class]) hasPrefix:@"SPSB"]) return; id firstResponder; for (UIWindow *window in jxh_application().windows) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" SEL f = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"first", @"Responder"]); firstResponder = [window performSelector:f]; #pragma clang diagnostic pop if (firstResponder) { [window endEditing:true]; break; } } } - (void)dealloc { [self removeKeyboardTool]; } @end