偷樑換柱 - iOS實現UITextField+Limit
在使用UITextField的過程中,不免會有限制字元個數,字元輸入規則的需求。一般情況下,會有如下兩種方法:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
ofollow,noindex">BNTextField-Limit 的方法
依然是利用block回撥,不過實現方式有點不同。
[testField limitCondition:^BOOL(NSString *inputStr){ return ![testField.text isEqualToString:@"111"]; } action:^{ NSLog(@"limit action"); }]; Or [testField limitNums:3 action:^{ NSLog(@"num limit action"); }]; 複製程式碼
BNTextField-Limit 的實現策略
對於UITextField用來做字元限制最好的方法就是使用- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
這個代理方法,我們通過判斷string來確定UITextField是否響應輸入。
接下來就是如何封裝好代理回撥的這個過程了這裡我借鑑了facebook /**KVOController **的思想,建立一箇中間管理者,來接管代理方法。 不過需要考慮幾個問題:
- 代理釋放的問題
- 多個條件約束
- 如何不影響其他代理方法
實現過程:
- 首先,我們假定,AController內實現了UITextField的delegate,我們先把delegate的身份接管過來,實現偷樑換柱 。
// self 即UITextField 這裡這是一個分類方法 self.delegate =UITextFieldDelegateManager.sharedInstance 複製程式碼
- UITextFieldDelegateManager為中間管理類, keyCode:
@interface UITextFieldDelegateManager : NSObject<UITextFieldDelegate> { NSMapTable<id,_LimitInfo *> *_infos; } + (instancetype)sharedInstance; - (void)addLimitNums:(NSInteger)num key:(id)key target:(id<UITextFieldDelegate>)target action:(void(^)(void))action; @end 複製程式碼
@interface _LimitInfo : NSObject @property(nonatomic,assign)NSInteger num; @property(nonatomic,weak)id<UITextFieldDelegate> pinocchio; @end 複製程式碼
這時,我們的UITextField的delegate成為了UITextFieldDelegateManager,這樣我們就“截獲”了AController的delgate身份。 而這裡有一個問題,那就是AController的UITextFieldDelegate內所有方法會失效,這個問題,我們稍後再說。
-
實現
- (void)addLimitNums:(NSInteger)num key:(id)key target:(id<UITextFieldDelegate>)target action:(void(^)(void))action;
_LimitInfo *info = [_infos objectForKey:key]; if (!info) { info = [_LimitInfo new]; info.pinocchio = target; } info.condition = condition; [info setConditionAction:action]; [_infos setObject:info forKey:key]; 複製程式碼
這裡Key是UITextField當前例項物件,target是AController,我們把這兩者對映進一個NSMapTable中,NSMapTable的弱引用會使我們不用擔心迴圈引用。其作用和字典一樣。
同時,也解決了多個條件約束的問題。
而_LimitInfo只是對AController的一個包裝,到這時,我們的AController已經被架空了,成為了一個受我們擺佈的傀儡:smirk:,pinocchio儲存了AController的例項。
- 接下來就簡單了,將UITextFieldDelegate在UITextFieldDelegateManager中全部實現出來主要是我們的shouldChangeCharactersInRange代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ BOOL checkInLimit = NO; _LimitInfo *info = [self safeReadForKey:textField]; if (info.condition && !info.condition(string) && string.length > 0) { info.conditionAction(); checkInLimit = YES; } if (info.num != 0) { if (info && textField.text.length == info.num && string.length > 0) { info.action(); checkInLimit = YES; } } if (checkInLimit) { return NO; } if (!info.pinocchio) { return YES; } return [info.pinocchio textField:textField shouldChangeCharactersInRange:range replacementString:string]; } 複製程式碼
其他方法類似,具體可以參見原始碼
不過這裡還要注意我們剛剛提到的問題,通過我們的pinocchioreturn [info.pinocchio textField:textField shouldChangeCharactersInRange:range replacementString:string];
來控制原本邏輯,不然delegate就失效了
至於代理釋放的問題,我是通過runtime hook UITextField的removeFromSuperview方法,在這個方法呼叫的時候,將pinocchio重新設定回UITextField的delegate,同時移除快取。
_LimitInfo* info = [_infos objectForKey:key]; ((UITextField*)key).delegate = info.pinocchio; [_infos removeObjectForKey:key]; 複製程式碼
- 至此,一個基於 facebook /KVOController 思想的小工具就出爐了,雖然簡單,但是需要這種思想還是比較巧妙的。