123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //
- // SPSBKeyboardManager.m
- // 我的社保
- //
- // Created by shanp on 2021/4/25.
- //
- #import "SPSBKeyboardManager.h"
- #import <JXHMacro.h>
- #import <JXHSystemShortcut.h>
- #import <objc/runtime.h>
- #import "SPSBTextField.h"
- #import <JXHGeneralTools.h>
- #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
|