偷梁换柱 - iOS实现UITextField+Limit

用例分析

在使用UITextField的过程当中,难免会有限制字符个数,字符输入规则的需求。通常状况下,会有以下两种方法:git

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

BNTextField-Limit的方法

依然是利用block回调,不过实现方式有点不一样。github

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

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

实现过程:

  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的弱引用会使咱们不用担忧循环引用。其做用和字典同样。ui

同时,也解决了多个条件约束的问题。atom

而_LimitInfo只是对AController的一个包装,到这时,咱们的AController已经被架空了,成为了一个受咱们摆布的傀儡😏,pinocchio保存了AController的实例。spa

  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就失效了code

至于代理释放的问题,我是经过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

相关文章
相关标签/搜索