我有一个应用程序,其中我想要输入前两个字符(仅限字母)然后空格然后接下来三个字符仅数字,然后空格和接下来的五个字符再次数字。
我知道必须在shouldchangecharacterinRange,textfield委托方法中完成,但是需要写的内容是我坚持的真实内容。我是ios的新手并且现在无法考虑它。 实现它的正确方法是什么。请提出建议。谢谢!
答案 0 :(得分:0)
你必须实现textfield委托方法[textfield shouldChangeCharacters replacementString] [1]
[1]:https://developer.apple.com/reference/uikit/uitextfielddelegate/1619599-textfield“”
答案 1 :(得分:0)
您需要编写代码以在textField:shouldChangeCharactersInRange:replacementString:
方法中实现您想要的任何格式。此方法在文本字段中每次用户编辑(键入/删除/粘贴)都会调用。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//range: range of character to be replaced.
// string : will give you replacement string--mostly one chracter if user is //typing
}
答案 2 :(得分:0)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
此代理将帮助您检查用户输入的字符取决于您要在文本字段中添加或不添加的字符 / ********编辑************ /
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.length==1) {
return YES;
}
if (textField.text.length<=12) {
if ((range.location==0)||(range.location==1))
{
if([@"QWERTYUIOPLKJHGFDSAZXCVBNM" containsString:[string uppercaseString]]){
return YES;
}
}
else if (range.location==2)
{
if ([@"0123456789" containsString:string ]) {
string = [NSString stringWithFormat:@" %@",string];
textField.text =[NSString stringWithFormat:@"%@%@",textField.text,string];
return NO;
}
else{
return NO;
}
}
else if(range.location<=5)
{
if ([@"0123456789" containsString:string ]) {
string = [NSString stringWithFormat:@" %@",string];
return YES;
}
else{
return NO;
}
}
else if (range.location>=6)
{
if (range.location==6) {
if ([@"0123456789" containsString:string ]) {
string = [NSString stringWithFormat:@" %@",string];
textField.text =[NSString stringWithFormat:@"%@%@",textField.text,string];
return NO;
}
}
else{
if ([@"0123456789" containsString:string ]) {
string = [NSString stringWithFormat:@" %@",string];
return YES;
}
else
{
return NO;
}
}
}
}
return NO;
}
答案 3 :(得分:0)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField== YourtextField)
{
if(range.length > 0)
NSString *str = @"Abcdefghijkl";
textField.text = [NSString stringWithFormat:@"%@",[str substringToIndex:2]];
}
if (textField==YourtextField) {
YourtextField.text = @"1234567890"
int length = (int) YourtextField.text;
if(length == 10)
{
if(range.length == 0)
return NO;
}
if(length == 3)
{
NSString *num = YourtextField.text;
textField.text = [NSString stringWithFormat:@"(%@) ",num];
if(range.length > 0)
textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
}
else if(length == 6)
{
NSString *num = textField.text;
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;
}