1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- //
- // SPSBTextField.m
- // 我的社保
- //
- // Created by shanp on 2021/4/21.
- //
- #import "SPSBTextField.h"
- #import <JXHMacro.h>
- #import <NSObject+JXHNotification.h>
- @implementation SPSBTextField
- - (instancetype)init {
- self = [super init];
- if (!self) return nil;
- @weakify(self)
- [self addNotificationName:UITextFieldTextDidChangeNotification block:^(id _Nullable object, NSDictionary * _Nullable userInfo) {
- @strongify(self)
- [self textFieldDidChange:object];
- }];
- return self;
- }
- - (void)textFieldDidChange:(id)sender {
- if (sender != self || _spsb_maxCount == 0) return;
- UITextField *textField = sender;
- NSString *toBeString = textField.text;
- UITextRange *selectedRange = [textField markedTextRange];
- // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
- if (!selectedRange) {
- if (toBeString.length > _spsb_maxCount) {
- textField.text = [toBeString substringToIndex:_spsb_maxCount];
- }
- }
- if (_spsb_afterJudge) {
- _spsb_afterJudge(self);
- }
- }
- - (void)textDidChange {
- [self textFieldDidChange:self];
- }
- - (void)dealloc {
- [self removeNotificationName:UITextFieldTextDidChangeNotification];
- }
- @end
|