IOS实现输入验证码、密码按位分割(二)

2020-01-14 18:45:20刘景俊
  • 若设置了验证码/密码的位数,和是否密文显示,则需要保持label中相应属性与vertificationCodeInputView中一致
    
    - (void)setNumberOfVertificationCode:(NSInteger)numberOfVertificationCode {
      _numberOfVertificationCode = numberOfVertificationCode;
      // 保持label的验证码/密码位数与IDVertificationCodeInputView一致,此时label一定已经被创建
      self.label.numberOfVertificationCode = _numberOfVertificationCode;
    }
    - (void)setSecureTextEntry:(bool)secureTextEntry {
      _secureTextEntry = secureTextEntry;
      self.label.secureTextEntry = _secureTextEntry;
    }
    

    4、弹出键盘,并接收键盘输入的字符

    弹出键盘

    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
      [self.textField becomeFirstResponder];
    }
    

    接收键盘输入的字符

    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
      // 判断是不是“删除”字符
      if (string.length != 0) {// 不是“删除”字符
        // 判断验证码/密码的位数是否达到预定的位数
        if (textField.text.length < self.numberOfVertificationCode) {
          self.label.text = [textField.text stringByAppendingString:string];
          self.vertificationCode = self.label.text;
          return YES;
        } else {
          return NO;
        }
      } else { // 是“删除”字符
        self.label.text = [textField.text substringToIndex:textField.text.length - 1];
        self.vertificationCode = self.label.text;
        return YES;
      }
    }
    

    5、绘制验证码/密码(IDLabel)

    手动调用drawRect方法

    
    //重写setText方法,当text改变时手动调用drawRect方法,将text的内容按指定的格式绘制到label上
    - (void)setText:(NSString *)text {
      [super setText:text];
      // 手动调用drawRect方法
      [self setNeedsDisplay];
    }
    

    绘制验证码/密码

    
    // 按照指定的格式绘制验证码/密码
    - (void)drawRect:(CGRect)rect {
      //计算每位验证码/密码的所在区域的宽和高
      float width = rect.size.width / (float)self.numberOfVertificationCode;;
      float height = rect.size.height;
      // 将每位验证码/密码绘制到指定区域
      for (int i = 0; i < self.text.length; i++) {
        // 计算每位验证码/密码的绘制区域
        CGRect tempRect = CGRectMake(i * width, 0, width, height);
        if (self.secureTextEntry) { // 密码,显示圆点
          UIImage *dotImage = [UIImage imageNamed:@"dot"];
          // 计算圆点的绘制区域
          CGPoint securityDotDrawStartPoint = CGPointMake(width * i + (width - dotImage.size.width) / 2.0, (tempRect.size.height - dotImage.size.height) / 2.0);
          // 绘制圆点
          [dotImage drawAtPoint:securityDotDrawStartPoint];
        } else { // 验证码,显示数字
          // 遍历验证码/密码的每个字符
          NSString *charecterString = [NSString stringWithFormat:@"%c", [self.text characterAtIndex:i]];
          // 设置验证码/密码的现实属性
          NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
          attributes[NSFontAttributeName] = self.font;
          // 计算每位验证码/密码的绘制起点(为了使验证码/密码位于tempRect的中部,不应该从tempRect的重点开始绘制)
          // 计算每位验证码/密码的在指定样式下的size
          CGSize characterSize = [charecterString sizeWithAttributes:attributes];
          CGPoint vertificationCodeDrawStartPoint = CGPointMake(width * i + (width - characterSize.width) / 2.0, (tempRect.size.height - characterSize.height) / 2.0);
          // 绘制验证码/密码
          [charecterString drawAtPoint:vertificationCodeDrawStartPoint withAttributes:attributes];
        }
      }
    }