在用户输入数据时格式化文本字段 [英] Format Text Field While User Enters Data
问题描述
我知道这是一些格式化的地方可以完成的
$ p $ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
任何帮助都会有很大帮助
好吧,这里是我现在的
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)范围replacementString:(NSString *)string
{
//格式出生日期YYYY-MM-DD
if(textField == txtDOB)
{
if((txtDOB.text.length == 4)||(txtDOB。 text.length == 7))
//处理backspace被按下
if(![string isEqualToString:@])
txtDOB.text = [txtDOB.text stringByAppendingString:@ - ];
return!([textField.text length]> 9&& [string length]> range.length);
}
else
return YES;
$ b $ p
$ b 唯一的问题是,当我尝试返回空格时,不工作我的猜测是因为它试图添加短划线,每次我打退格键。任何人都知道解决办法?
so here is what I am trying to do. I have a birth date text field, while the user in puts text i want the field to automatically put it in the form YYYY-MM-DD. Basically while the user types it replaces the Y's M's and D's but leaves the hyphen. I am not sure how to go about this i may need a mask or something.
I know this is where some of the formatting can be done
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
Any help would be of much help
解决方案 OK so here is what i have now
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Format Date of Birth YYYY-MM-DD
if(textField == txtDOB)
{
if ((txtDOB.text.length == 4)||(txtDOB.text.length == 7))
//Handle backspace being pressed
if (![string isEqualToString:@""])
txtDOB.text = [txtDOB.text stringByAppendingString:@"-"];
return !([textField.text length]>9 && [string length] > range.length);
}
else
return YES;
}
Only problem is that when i try to back space, it doesn't work my guess is because it is trying to add the dash every time i hit the backspace key. Anyone know a work around?
这篇关于在用户输入数据时格式化文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!