gpt4 book ai didi

ios - 我有很多格式,如 ####-##-##、(###)-###-####,我想将文本字段的值更改为给定格式,如何实现。?

转载 作者:行者123 更新时间:2023-11-29 00:30:05 26 4
gpt4 key购买 nike

我有很多格式,如####-##-##、(###)-###-####,我想将文本字段的值更改为给定格式,如何实现.?

一种方法是手动检查字符串并将其更改为字符串,据我所知,还有其他有效的方法可以做到这一点吗?请让我,我正在为文本字段获取不同的格式,例如:

**Format1** for Textfield1 : ####-###-### [I want to convert enter text of field to this format like 3333-333-333]
**Format2** for Textfield2 : (###)-###-#### [I want to convert enter text of field to this format like (333)-333-3333]
**Format3** for Textfield3 : ##-##-## [I want to convert enter text of field to this format like 33-33-33]

等如果有一些有效的方法来处理它,请告诉我。谢谢!

最佳答案

格式 2

在文本字段委托(delegate)方法中添加 if 条件 shouldChangeCharactersInRange:

 if (textField == self.addressPhoneNumberTextField) {
int length = (int)[self getLength:textField.text];
//NSLog(@"Length = %d ",length);

NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
return NO;
}

if(length == 10)
{
if(range.length == 0)
return NO;
}

if(length == 3)
{
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:@"(%@) ",num];

if(range.length > 0)
textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
}
else if(length == 6)
{
NSString *num = [self formatNumber:textField.text];
//NSLog(@"%@",[num substringToIndex:3]);
//NSLog(@"%@",[num substringFromIndex:3]);
textField.text = [NSString stringWithFormat:@"(%@) %@-",[num substringToIndex:3],[num substringFromIndex:3]];

if(range.length > 0)
textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
}

return YES;
}

- (NSString *)formatNumber:(NSString *)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

int length = (int)[mobileNumber length];
if(length > 10)
{
mobileNumber = [mobileNumber substringFromIndex: length-10];

}

return mobileNumber;
}

- (int)getLength:(NSString *)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

int length = (int)[mobileNumber length];

return length;
}

关于ios - 我有很多格式,如 ####-##-##、(###)-###-####,我想将文本字段的值更改为给定格式,如何实现。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com