QGPassWordTextField.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // PassWord.m
  3. // PayPassWord
  4. //
  5. // Created by mac on 16/1/14.
  6. // Copyright © 2016年 mac. All rights reserved.
  7. //
  8. #import "QGPassWordTextField.h"
  9. @implementation QGPassWordTextField
  10. static NSString * tempStr;
  11. - (void)drawRect:(CGRect)rect {
  12. /** 计算坐标 */
  13. CGFloat y = 0;
  14. CGFloat width = rect.size.width / self.passWordCount;
  15. CGFloat height = rect.size.height;
  16. CGFloat margin = rect.size.height / 3.0 * 2;
  17. /** 创建存储坐标的数组 */
  18. NSMutableArray <NSValue *>*frameArr = [NSMutableArray array];
  19. /** 开启位图上下文 循环并绘制竖线*/
  20. CGContextRef context = UIGraphicsGetCurrentContext();
  21. for (int i = 0; i < self.passWordCount; i++) {
  22. [[UIColor grayColor]set];
  23. CGContextAddRect(context, CGRectMake(i * (width), y, width, height));
  24. [frameArr addObject:[NSValue valueWithCGRect:CGRectMake(i * (width) + margin, y + margin, width - 2 * margin, height - 2 * margin)]];
  25. if (_showNumber || _showHorizontal) {
  26. UILabel *label = labelArray[i];
  27. label.frame = CGRectMake(i * (width) + margin, y + margin, width - 2 * margin, height - 2 * margin);
  28. if (_showHorizontal) {
  29. label.text = @"-";
  30. }else {
  31. label.text = @"";
  32. }
  33. }
  34. }
  35. CGContextStrokePath(context);
  36. /** 监听文字变化 */
  37. [self addTarget:self action:@selector(passWordDidChange:) forControlEvents:UIControlEventEditingChanged];
  38. /** 监听文字个数变化,并且循环并绘制实心圆 */
  39. if (self.text.length) {
  40. for (int i = 0; i < self.text.length; i++) {
  41. if (self.showNumber) {
  42. UILabel *label = labelArray[i];
  43. label.text = [self.text substringWithRange:NSMakeRange(i, 1)];
  44. }else {
  45. [[UIColor blackColor]set];
  46. CGContextFillEllipseInRect(context, [frameArr[i] CGRectValue]);
  47. }
  48. //如果输入够了
  49. if (_notJudgeComplete) {
  50. if (FinishInputBlock) {
  51. FinishInputBlock(self.text);
  52. }
  53. }else {//下边两个后边的条件可以去掉
  54. if (i == self.passWordCount - 1 && self.text.length == self.passWordCount) {
  55. if (FinishInputBlock) {
  56. FinishInputBlock(self.text);
  57. }
  58. }
  59. }
  60. }
  61. }else{
  62. return;
  63. }
  64. }
  65. //如果输入位数够了 回调de一个方法
  66. -(void)finishInput:(MyBlockType)block
  67. {
  68. FinishInputBlock = block;
  69. }
  70. /** 监听文字的变化 */
  71. - (void)passWordDidChange:(UITextField *)sender{
  72. if (sender.text.length > self.passWordCount) {
  73. NSRange range = NSMakeRange(0, self.passWordCount);
  74. sender.text = [sender.text substringWithRange:range];
  75. }
  76. [self setNeedsDisplay];
  77. }
  78. #pragma mark - 重写的方法
  79. /** 重写 setPlaceholder 方法 防止出现 placeHolder*/
  80. -(void)setPlaceholder:(NSString *)placeholder{
  81. }
  82. /** 重写 initWithFrame 方法 防止出现光标*/ //牛逼
  83. - (instancetype)initWithFrame:(CGRect)frame
  84. {
  85. self = [super initWithFrame:frame];
  86. if (self) {
  87. self.tintColor = [UIColor clearColor];
  88. self.textColor = [UIColor clearColor];
  89. self.keyboardType = UIKeyboardTypeNumberPad;
  90. self.passWordCount = 6;
  91. labelArray = [NSMutableArray array];
  92. for (int i = 0; i < 10; i ++) {//创建10个 多出来的备用
  93. UILabel *label = [[UILabel alloc] init];
  94. [label setText:@"" Font:FontLarger TextColor:[UIColor blackColor] Alignment:NSTextAlignmentCenter];
  95. [self addSubview:label];
  96. [labelArray addObject:label];
  97. }
  98. }
  99. return self;
  100. }
  101. /** 重写文字颜色方法 让文字永远是透明色 */
  102. -(void)setTextColor:(UIColor *)textColor{
  103. textColor = [UIColor clearColor];
  104. [super setTextColor:textColor];
  105. }
  106. /** 重写光标的颜色方法 让光标永远是透明色 */
  107. -(void)setTintColor:(UIColor *)tintColor{
  108. tintColor = [UIColor clearColor];
  109. [super setTintColor:tintColor];
  110. }
  111. /** 禁用复制粘贴的功能 */
  112. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
  113. return NO;
  114. }
  115. /** 重写键盘类型 */
  116. -(void)setKeyboardType:(UIKeyboardType)keyboardType{
  117. keyboardType = UIKeyboardTypeNumberPad;
  118. [super setKeyboardType:keyboardType];
  119. }
  120. @end