成都网站建设设计

将想法与焦点和您一起共享

iosUITextField输入框,几位数字后面加什么字符

@interface ViewController ()

创新互联公司专业为企业提供郑州网站建设、郑州做网站、郑州网站设计、郑州网站制作等企业网站建设、网页设计与制作、郑州企业网站模板建站服务,十多年郑州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。


{

    UITextField    *    _numberFielde;

}



@end


@implementation ViewController


static  NSString * NUMBER = @"0123456789";//数字

static  int  setNumber = 1; //几个数字加空格 1改成自己想要的字符

static  NSString * kongSring = @"*";//几个数字中间加的字符 x改成自己想要的字符



- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    _numberFielde = [[UITextField alloc] initWithFrame:CGRectMake(10, 100,self.view.frame.size.width-20, 35)];

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

    _numberFielde.layer.borderWidth = 1.0f;

    _numberFielde.delegate = self;

    [_numberFielde becomeFirstResponder];

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

    [self.view addSubview:_numberFielde];

    // Do any additional setup after loading the view, typically from a nib.

}


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

    


    

    NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:NUMBER] invertedSet]; //定义一个字符集合 

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

    BOOL basicTest = [string isEqualToString:filtered];

    if(basicTest)

    {

      if (textField == _numberFielde)

      {

        //setNumber位加一个空格

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

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

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

            }

            return YES;

        } else {

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

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

            }

        }

        return YES;

      }

    }

    else

    {

        return NO;

    }

    return YES;

}


当前名称:iosUITextField输入框,几位数字后面加什么字符
当前链接:http://chengdu.cdxwcx.cn/article/ieccjg.html