RAC限制UITextFeild字数,格式化输入的手机号码(3-4-4)

前提:要求输入框只能输入手机号码,手机号码的格式(3-4-4),同时控制获取验证码事件是否能交互。

使用Cocoapods来添加依赖

此处省略如何使用Cocoapods来管理

pod 'ReactiveCocoa'

如何使用


   RACSignal *phoneSignal  = self.phoneText.rac_textSignal;
    
    [phoneSignal subscribeNext:^(id x) {
        NSString *phoneString = [self phoneFormatter:x];
        
        if (phoneLogin.length >=14) {
            self.phoneText.text = [phoneLogin substringToIndex:13];
        }else
        {
            self.phoneText.text = phoneLogin;
        }
        
    }];

//格式化手机号码
- (NSString*)phoneFormatter:(NSString *)phone{
    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
    phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
    // 如果是电话号码格式化,需要添加这三行代码
    NSMutableString *temString = [NSMutableString stringWithString:phone];
    [temString insertString:@" " atIndex:0];
    phone = temString;
    NSString *newString = @"";
    while (newString.length > 0) {
        NSString *subString = [phone substringToIndex:MIN(phone.length, 4)];
        newString = [newString stringByAppendingString:subString];
        if (subString.length == 4) {
            newString = [newString stringByAppendingString:@" "];
        }
        newString = [newString substringFromIndex:MIN(phone.length, 4)];
    }
    newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
    return newString;
}

*根据输入框来控制 Button交互


    RAC(self.sureButton,enabled) = [RACSignal combineLatest:@[phoneSignal,self.codeText.rac_textSignal] reduce:^(NSString *phoneString,NSString *codeString){
        
        phoneString = [phoneString stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        return @(phoneString.length ==11 && codeString.length >0);
    }];
  [RACObserve(self.sureButton, enabled) subscribeNext:^(NSNumber *loggedIn) {
        if (loggedIn.boolValue) {
            self.sureButton.backgroundColor = [UIColor colorWithHexInt:0xf48400];
        }else
        {
            self.sureButton.backgroundColor = [UIColor colorWithHexInt:0xcccccc];
        }
    }];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,222评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,625评论 25 709
  • 你身边是否有这样一群女子, 她们少女以上,熟女未满。 良好的学识,丰富的阅历,稳定的收入, 令她们对许多事都得之坦...
    粒上皇阅读 1,482评论 0 0
  • 每天早上都有一些鸟儿站在别人家的楼顶上未锯掉的铁杆上鸣叫,有的是黑色的,有的灰色,有的很娇小,有的脑袋上顶着块白斑...
    手绘者焕新阅读 2,338评论 0 2
  • API: 管理员登录 URL: /admin_login_background/ 请求方式: POST 参数 返...
    一曲广陵散阅读 2,981评论 0 0