一.体验步骤:
- 准备工作
|
|
- 开始体验(编写一个类似密码输入的UI:但并不是)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081821. 首先导入头文件:2. 代码正文:- (void)viewDidLoad {[super viewDidLoad];[self firstRAC];}- (void)firstRAC {UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.center.x - 100, 100, 200, 40)];[self.view addSubview:textField];textField.borderStyle = UITextBorderStyleRoundedRect;textField.text = @"";textField.placeholder = @"请输入密码";textField.secureTextEntry = YES;NSMutableArray <UILabel *>*arrayM = [NSMutableArray array];for (NSInteger i = 0; i<=3; i++) {UILabel *label = [[UILabel alloc]init];label.tag = i;label.backgroundColor = [UIColor clearColor];[self.view addSubview:label];label.frame = CGRectMake(self.view.center.x + i*51 - 100, 150, 47, 10);[arrayM addObject:label];}UILabel *tipLabel = [[UILabel alloc]init];[self.view addSubview:tipLabel];tipLabel.frame = CGRectMake(self.view.center.x - 100, 170, 200, 10);tipLabel.font = [UIFont systemFontOfSize:11];tipLabel.textColor = [UIColor greenColor];tipLabel.text = @"";///核心代码[[textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x){NSLog(@"textField.text========%@", textField.text);NSInteger j = textField.text.length;NSInteger k = 0;if (j <= 0) {tipLabel.text = @"";k=0;}else if (j>0 && j<=6) {k = 1;}else if (j>6 && j<=9) {k=2;}else if (j>9 && j<=12) {k=3;}else if (j>12 && j<=15) {k=4;}else {return ;}for (NSInteger i = 0; i < 4; i++) {if (i <= k - 1) {arrayM[i].backgroundColor = [UIColor magentaColor];if (k == 1) {tipLabel.text = @"密码太弱";tipLabel.textColor = [UIColor grayColor];}else if (k==2) {tipLabel.text = @"密码一般";tipLabel.textColor = [UIColor orangeColor];}else if (k==3) {tipLabel.text = @"密码较强";tipLabel.textColor = [UIColor redColor];}else if (k==4) {tipLabel.text = @"密码非常强";tipLabel.textColor = [UIColor purpleColor];}}else {arrayM[i].backgroundColor = [UIColor clearColor];}}}];}
——–
效果图: