@interface ViewController ()
{
UITextField * _numberFielde;
}
@end
@implementation ViewController
static NSString * NUMBER = @"0123456789";//數(shù)字
static int setNumber = 1; //幾個(gè)數(shù)字加空格 1改成自己想要的字符
static NSString * kongSring = @"*";//幾個(gè)數(shù)字中間加的字符 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 = @"請(qǐng)輸入數(shù)字";
[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]; //定義一個(gè)字符集合
NSString*filtered = [[string componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if(basicTest)
{
if (textField == _numberFielde)
{
//setNumber位加一個(gè)空格
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;
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。