让UITextFeld输入的时候和输入银行卡一样的格式

来源:互联网 发布:菲诗小铺的洗发水知乎 编辑:程序博客网 时间:2024/05/22 12:49

#import "ViewController.h"

#define NUMBERS @"0123456789"


@interface ViewController () <UITextFieldDelegate> {

    UITextField * _numberFielde;

    UILabel *_numLabel;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    

    [super viewDidLoad];

    

    _numberFielde = [[UITextField alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, 50)];

    _numberFielde.layer.borderColor = [UIColor grayColor].CGColor;

    _numberFielde.layer.borderWidth = 1.0f;

    _numberFielde.delegate = self;

    _numberFielde.placeholder = @"请输入数字";

    [_numberFielde becomeFirstResponder];

    

    [self.view addSubview:_numberFielde];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(100, 200, 100, 30);

    [button addTarget:self action:@selector(getNum) forControlEvents:UIControlEventTouchUpInside];

    [button setTitle:@"取得数字" forState:UIControlStateNormal];

    

    [self.view addSubview:button];

    

    _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, self.view.frame.size.width, 50)];

    _numLabel.textAlignment = NSTextAlignmentCenter;

    

    [self.view addSubview:_numLabel];

}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    

    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];

    

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    

    BOOL basicTest = [string isEqualToString:filtered];

    

    if (basicTest) {

        if (textField == _numberFielde) {

            

            //四位加一个空格

            if ([string isEqualToString:@""]) { //删除字符

                

                if ((textField.text.length - 2) % 5 == 0) {

                    textField.text = [textField.text substringToIndex:textField.text.length - 1];

                }

                return YES;

            } else {

                if (textField.text.length % 5 == 0) {

                    textField.text = [NSString stringWithFormat:@"%@ ", textField.text];

                }

            }

            return YES;

        }

    } else {

        return NO;

    }

    

    return YES;

}


- (void)getNum {

    _numLabel.text = [self deleteSpace];

}



- (NSString *)deleteSpace {

    NSString *username =_numberFielde.text;

    username =  [username stringByReplacingOccurrencesOfString:@" " withString:@""];

    return username;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}


@end

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网购东西发错了怎么办 拒签商家不退款怎么办 红米手机无响应怎么办 淘宝网登录密码忘记了怎么办 淘宝网密码忘记了怎么办 京东抢购不发货怎么办 微商不想做了怎么办 减肥过程中饿了怎么办 滴滴车龄超过6年怎么办 网络公选课没过怎么办 大学网络课挂了怎么办 淘宝虚拟订单买家恶意退款怎么办 淘宝卖家虚拟发货怎么办 淘宝买虚拟产品被骗了怎么办 哈尔滨暖气低于十八度怎么办 淘客店铺没人买怎么办 淘宝商家不给退货怎么办 淘宝卖家拒绝退款申请怎么办 运费险赔付少了怎么办 买了运费险退货怎么办 卖家运费险退货怎么办 京东生鲜有坏的怎么办 与上级意见不一致时你将怎么办 物金所倒闭投资怎么办 电商平台欺骗客户怎么办 pdf电脑打开是乱码怎么办 excel表格打开是乱码怎么办 win10安装软件出现乱码怎么办 华为手机速度越来越慢怎么办 oppo手机速度越来越慢怎么办 安卓手机速度越来越慢怎么办 青桔单车忘了锁怎么办 华为手机反应太慢了怎么办 魅族关机键失灵怎么办 oppa79手机开不开机怎么办 黑衣服洗完发白怎么办 白衣服被黑衣服染色了怎么办 评职称单位领导不推荐怎么办 支付宝被限制收款怎么办 在淘宝上下单想写两个地址怎么办 注销了的支付宝怎么办