用例分析
在使用UITextField的過程中,不免會有限制字符個數(shù),字符輸入規(guī)則的需求。一般情況下,會有如下兩種方法:
- 直接設(shè)置代理,實現(xiàn)代理方法,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string - 封裝代理過程,利用block來實現(xiàn)回調(diào)
- 當(dāng)然方法不止這兩種,這里只是舉常用的例子,不再贅述
BNTextField-Limit的方法
依然是利用block回調(diào),不過實現(xiàn)方式有點(diǎn)不同。
[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的實現(xiàn)策略
對于UITextField用來做字符限制最好的方法就是使用- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string這個代理方法,我們通過判斷string來確定UITextField是否響應(yīng)輸入。
接下來就是如何封裝好代理回調(diào)的這個過程了
這里我借鑒了 facebook/KVOController的思想,創(chuàng)建一個中間管理者,來接管代理方法。
不過需要考慮幾個問題:
- 代理釋放的問題
- 多個條件約束
- 如何不影響其他代理方法
實現(xiàn)過程:
- 首先,我們假定,AController內(nèi)實現(xiàn)了UITextField的delegate,我們先把delegate的身份接管過來,實現(xiàn)偷梁換柱。
// 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內(nèi)所有方法會失效,這個問題,我們稍后再說。
- 實現(xiàn)
- (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當(dāng)前實例對象,target是AController,我們把這兩者映射進(jìn)一個NSMapTable中,NSMapTable的弱引用會使我們不用擔(dān)心循環(huán)引用。其作用和字典一樣。
同時,也解決了多個條件約束的問題。
而_LimitInfo只是對AController的一個包裝,到這時,我們的AController已經(jīng)被架空了,成為了一個受我們擺布的傀儡??,pinocchio保存了AController的實例。
- 接下來就簡單了,將UITextFieldDelegate在UITextFieldDelegateManager中全部實現(xiàn)出來
主要是我們的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];
}
其他方法類似,具體可以參見源碼
不過這里還要注意我們剛剛提到的問題,通過我們的pinocchio return [info.pinocchio textField:textField shouldChangeCharactersInRange:range replacementString:string];來控制原本邏輯,不然delegate就失效了
至于代理釋放的問題,我是通過runtime hook UITextField的removeFromSuperview方法,在這個方法調(diào)用的時候,將pinocchio重新設(shè)置回UITextField的delegate,同時移除緩存。
_LimitInfo* info = [_infos objectForKey:key];
((UITextField*)key).delegate = info.pinocchio;
[_infos removeObjectForKey:key];
- 至此,一個基于 facebook/KVOController思想的小工具就出爐了,雖然簡單,但是需要這種思想還是比較巧妙的。