【问题标题】:How to enter the textfield.text in a special format如何以特殊格式输入 textfield.text
【发布时间】:2017-04-25 12:03:20
【问题描述】:

我有一个应用程序,我想在其中输入前两个字符(仅限字母),然后输入空格,然后输入接下来的三个数字字符,然后再输入空格和接下来的五个数字字符。

我知道它必须在 shouldchangecharacterinRange,textfield 委托方法中完成,但里面需要写的是我坚持的真实内容。我是 ios 的新手,现在无法考虑。 实现它的正确方法是什么。请提出建议。在此先感谢!

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    您必须实现文本字段委托方法 [textfield shouldChangeCharacters replacementString][1]

    [1]:https://developer.apple.com/reference/uikit/uitextfielddelegate/1619599-textfield""

    【讨论】:

      【解决方案2】:

      您需要编写代码以在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 
      }
      

      【讨论】:

      • 我知道这一点,但实际上我需要帮助来编写此方法中的代码,因为我对 ios 太陌生,无法理解需要在其中完成的事情,以便获得所需的文本字段格式.text可以实现。
      【解决方案3】:
       - (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;
      }
      

      【讨论】:

        【解决方案4】:
        - (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;
        }
        

        【讨论】:

        • 您能解释一下这有助于在打字时格式化文本吗?
        • 请检查 Ans 中的更新
        • 我已经把这个作为答案了,我只是不明白你的第一个答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-11
        • 2018-05-07
        • 1970-01-01
        • 2021-02-12
        • 1970-01-01
        相关资源
        最近更新 更多