偷梁换柱 - iOS实现UITextField+Limit

2,131 阅读3分钟

用例分析

在使用UITextField的过程中,不免会有限制字符个数,字符输入规则的需求。一般情况下,会有如下两种方法:

  • 直接设置代理,实现代理方法,- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  • 封装代理过程,利用block来实现回调
  • 当然方法不止这两种,这里只是举常用的例子,不再赘述

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**的思想,创建一个中间管理者,来接管代理方法。 不过需要考虑几个问题:

  • 代理释放的问题
  • 多个条件约束
  • 如何不影响其他代理方法

实现过程:

  1. 首先,我们假定,AController内实现了UITextField的delegate,我们先把delegate的身份接管过来,实现偷梁换柱
// self 即UITextField 这里这是一个分类方法
  self.delegate =  UITextFieldDelegateManager.sharedInstance
  1. 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内所有方法会失效,这个问题,我们稍后再说。

  1. 实现- (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已经被架空了,成为了一个受我们摆布的傀儡😏,pinocchio保存了AController的实例。

  1. 接下来就简单了,将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];
}

其他方法类似,具体可以参见源码

不过这里还要注意我们刚刚提到的问题,通过我们的pinocchio return [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];
  1. 至此,一个基于 facebook/KVOController思想的小工具就出炉了,虽然简单,但是需要这种思想还是比较巧妙的。

最后贴出源码地址,欢迎指正# BNTextField-Limit