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;
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:iosUITextField输入框,几位数字后面加什么字符-创新互联
浏览路径:http://myzitong.com/article/ddppgi.html