123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //
- // PassWord.m
- // PayPassWord
- //
- // Created by mac on 16/1/14.
- // Copyright © 2016年 mac. All rights reserved.
- //
- #import "QGPassWordTextField.h"
- @implementation QGPassWordTextField
- static NSString * tempStr;
- - (void)drawRect:(CGRect)rect {
- /** 计算坐标 */
- CGFloat y = 0;
- CGFloat width = rect.size.width / self.passWordCount;
- CGFloat height = rect.size.height;
- CGFloat margin = rect.size.height / 3.0 * 2;
-
- /** 创建存储坐标的数组 */
- NSMutableArray <NSValue *>*frameArr = [NSMutableArray array];
-
- /** 开启位图上下文 循环并绘制竖线*/
- CGContextRef context = UIGraphicsGetCurrentContext();
- for (int i = 0; i < self.passWordCount; i++) {
- [[UIColor grayColor]set];
- CGContextAddRect(context, CGRectMake(i * (width), y, width, height));
- [frameArr addObject:[NSValue valueWithCGRect:CGRectMake(i * (width) + margin, y + margin, width - 2 * margin, height - 2 * margin)]];
-
- if (_showNumber || _showHorizontal) {
- UILabel *label = labelArray[i];
- label.frame = CGRectMake(i * (width) + margin, y + margin, width - 2 * margin, height - 2 * margin);
- if (_showHorizontal) {
- label.text = @"-";
- }else {
- label.text = @"";
- }
- }
- }
- CGContextStrokePath(context);
-
- /** 监听文字变化 */
- [self addTarget:self action:@selector(passWordDidChange:) forControlEvents:UIControlEventEditingChanged];
-
- /** 监听文字个数变化,并且循环并绘制实心圆 */
- if (self.text.length) {
- for (int i = 0; i < self.text.length; i++) {
- if (self.showNumber) {
- UILabel *label = labelArray[i];
- label.text = [self.text substringWithRange:NSMakeRange(i, 1)];
- }else {
- [[UIColor blackColor]set];
- CGContextFillEllipseInRect(context, [frameArr[i] CGRectValue]);
- }
-
- //如果输入够了
- if (_notJudgeComplete) {
- if (FinishInputBlock) {
- FinishInputBlock(self.text);
- }
- }else {//下边两个后边的条件可以去掉
- if (i == self.passWordCount - 1 && self.text.length == self.passWordCount) {
- if (FinishInputBlock) {
- FinishInputBlock(self.text);
- }
- }
- }
-
- }
- }else{
- return;
- }
-
- }
- //如果输入位数够了 回调de一个方法
- -(void)finishInput:(MyBlockType)block
- {
- FinishInputBlock = block;
- }
- /** 监听文字的变化 */
- - (void)passWordDidChange:(UITextField *)sender{
- if (sender.text.length > self.passWordCount) {
- NSRange range = NSMakeRange(0, self.passWordCount);
- sender.text = [sender.text substringWithRange:range];
- }
- [self setNeedsDisplay];
- }
- #pragma mark - 重写的方法
- /** 重写 setPlaceholder 方法 防止出现 placeHolder*/
- -(void)setPlaceholder:(NSString *)placeholder{
- }
- /** 重写 initWithFrame 方法 防止出现光标*/ //牛逼
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.tintColor = [UIColor clearColor];
- self.textColor = [UIColor clearColor];
- self.keyboardType = UIKeyboardTypeNumberPad;
- self.passWordCount = 6;
- labelArray = [NSMutableArray array];
- for (int i = 0; i < 10; i ++) {//创建10个 多出来的备用
- UILabel *label = [[UILabel alloc] init];
- [label setText:@"" Font:FontLarger TextColor:[UIColor blackColor] Alignment:NSTextAlignmentCenter];
- [self addSubview:label];
- [labelArray addObject:label];
- }
- }
- return self;
- }
- /** 重写文字颜色方法 让文字永远是透明色 */
- -(void)setTextColor:(UIColor *)textColor{
- textColor = [UIColor clearColor];
- [super setTextColor:textColor];
- }
- /** 重写光标的颜色方法 让光标永远是透明色 */
- -(void)setTintColor:(UIColor *)tintColor{
- tintColor = [UIColor clearColor];
- [super setTintColor:tintColor];
- }
- /** 禁用复制粘贴的功能 */
- - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
- return NO;
- }
- /** 重写键盘类型 */
- -(void)setKeyboardType:(UIKeyboardType)keyboardType{
- keyboardType = UIKeyboardTypeNumberPad;
- [super setKeyboardType:keyboardType];
- }
- @end
|