美文网首页
OC 控件 TextField的常用知识

OC 控件 TextField的常用知识

作者: 冯汉栩 | 来源:发表于2021-07-19 11:30 被阅读0次

    一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。

    1.TextField 左边边距

    self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 0)];
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    

    2.获取输入的值

    UITextFieldDelegate
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
      NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
      NSLog(@"----- %@",newText);
      return YES;
    }
    

    3.去除左右内容中的空格

     NSString *str = [self.Field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    

    4.判断内容是否全部是空格

    //看剩下的字符串的长度是否为零
    NSString *temp = [tf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([temp length]== 0) {
    }
    

    5.只允许输入字母和数字,限制长度


    image.png
    #pragma mark - 输入框发生改变就触发
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
      if ([textField isEqual:self.phoneTextField]) {//手机框
        self.previousSelection = textField.selectedTextRange;
        self.previousTextFieldContent = textField.text;
          return YES;
      }else if ([textField isEqual:self.VCodeTextField]){//验证码框
          self.VCode = [textField.text stringByReplacingCharactersInRange:range withString:string];
          [self controlNextBtnByPhoneNum:self.phoneCode];
          return YES;
      }else if ([textField isEqual:self.passwordTextField]){//密码框
          //原理就是把输入的信息转换成ASCII值  查看是否符合要求
          NSUInteger lengthOfString = string.length;  //lengthOfString的值始终为1
          for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
              unichar character = [string characterAtIndex:loopIndex]; //将输入的值转化为ASCII值(即内部索引值),可以参考ASCII表
              // 48-57;{0,9};65-90;{A..Z};97-122:{a..z}
              if (character < 48){ return NO; }
              if (character > 57 && character < 65){ return NO; }
              if (character > 90 && character < 97){ return NO; }
              if (character > 122){ return NO; }
          }
          NSUInteger proposedNewLength = textField.text.length - range.length + string.length;//检测总长度
          if (proposedNewLength > 16) { return NO; }//限制长度
          //上面的情况都是不合格的   返回NO
          self.pwdCode = [textField.text stringByReplacingCharactersInRange:range withString:string];
          [self controlNextBtnByPhoneNum:self.phoneCode];
          return YES;
      }else{
          return NO;
      }
    }
    

    相关文章

      网友评论

          本文标题:OC 控件 TextField的常用知识

          本文链接:https://www.haomeiwen.com/subject/yubcmltx.html