本文提供的是UITextField切換密碼輸入明暗文的一個(gè)分類(lèi),拿到項(xiàng)目中可以直接使用
.h文件
@interface UITextField (Extention)
//設(shè)置密碼輸入框
- (void)setTextFieldWithFont:(CGFloat)font color:(UIColor *)color alignment:(NSTextAlignment)alignment title:(NSString *)title placeHolder:(NSString *)placeholder;
@end
.m文件
#import "UITextField+Extention.h"
@implementation UITextField (Extention)
- (void)setTextFieldWithFont:(CGFloat)font color:(UIColor *)color alignment:(NSTextAlignment)alignment title:(NSString *)title placeHolder:(NSString *)placeholder{
UIButton *rightImageV = [[UIButton alloc] init];
self.secureTextEntry = YES;
[rightImageV setBackgroundImage:[UIImage imageNamed:@"me_invisible"] forState:UIControlStateNormal];
rightImageV.frame = CGRectMake(0, 0, 23, 23);
rightImageV.centerY = self.centerY;
self.rightView = rightImageV;
self.rightViewMode = UITextFieldViewModeAlways;
[rightImageV addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
self.font = [UIFont systemFontOfSize:font];
self.textColor = color == nil ? [UIColor blackColor] : color;
self.textAlignment = alignment;
self.borderStyle = UITextBorderStyleNone;
self.text = title == nil ? @"" : title;
self.placeholder = placeholder == nil ? @"" : placeholder;
}
//監(jiān)聽(tīng)右邊按鈕的點(diǎn)擊,切換密碼輸入明暗文狀態(tài)
-(void)btnClick:(UIButton *)btn{
//解決明暗文切換后面空格的問(wèn)題的兩種方式
//NSString* text = self.text;
//self.text = @" ";
//self.text = text;
//[self becomeFirstResponder];
[self resignFirstResponder];//取消第一響應(yīng)者
btn.selected = !btn.selected;
if (!btn.selected) {
self.font = [UIFont systemFontOfSize:16];
[btn setBackgroundImage:[UIImage imageNamed:@"me_invisible"] forState:UIControlStateNormal];
self.secureTextEntry = YES;
}else{
self.font = [UIFont systemFontOfSize:16];
[btn setBackgroundImage:[UIImage imageNamed:@"me_visible"] forState:UIControlStateSelected];
self.secureTextEntry = NO;
}
[self becomeFirstResponder];//放棄第一響應(yīng)者
}
@end
效果圖
ps:UITextField其他常用的分類(lèi)類(lèi)方法
// 設(shè)置可以帶背景圖片的文本框
+ (instancetype)setTextFieldWithFont:(CGFloat)font color:(UIColor *)color alignment:(NSTextAlignment)alignment backgroupImage:(UIImage *)image title:(NSString *)title placeHolder:(NSString *)placeholder
{
UITextField *textField = [[UITextField alloc] init];
textField.font = [UIFont systemFontOfSize:font];
textField.textColor = color == nil ? [UIColor blackColor] : color;
textField.textAlignment = alignment;
textField.borderStyle = UITextBorderStyleNone;
if (image) {
textField.background = image;
}
textField.text = title == nil ? @"" : title;
textField.placeholder = placeholder == nil ? @"" : placeholder;
return textField;
}
// 設(shè)置右邊可以帶圖片的文本框
+ (instancetype)setTextFieldWithFont:(CGFloat)font color:(UIColor *)color alignment:(NSTextAlignment)alignment rightImage:(UIImage *)image title:(NSString *)title placeHolder:(NSString *)placeholder{
UITextField *textField = [[UITextField alloc] init];
textField.font = [UIFont systemFontOfSize:font];
textField.textColor = color == nil ? [UIColor blackColor] : color;
textField.textAlignment = alignment;
textField.borderStyle = UITextBorderStyleNone;
textField.text = title == nil ? @"" : title;
textField.placeholder = placeholder == nil ? @"" : placeholder;
UIImageView *rightImageV = [[UIImageView alloc] initWithImage:image];
rightImageV.frame = CGRectMake(0, 0, 23, 23);
rightImageV.centerY = textField.centerY;
textField.rightView = rightImageV;
textField.rightViewMode = UITextFieldViewModeAlways;
return textField;
}
+ (instancetype)textFieldWithFont:(UIFont *)font color:(UIColor *)color alignment:(NSTextAlignment)alignment rightImage:(UIImage *)image title:(NSString *)title placeHolder:(NSString *)placeholder{
UITextField *textField = [[UITextField alloc] init];
textField.font = font;
textField.textColor = color == nil ? [UIColor blackColor] : color;
textField.textAlignment = alignment;
textField.borderStyle = UITextBorderStyleNone;
textField.text = title == nil ? @"" : title;
textField.placeholder = placeholder == nil ? @"" : placeholder;
UIImageView *rightImageV = [[UIImageView alloc] initWithImage:image];
rightImageV.frame = CGRectMake(0, 0, 23, 23);
rightImageV.centerY = textField.centerY;
textField.rightView = rightImageV;
textField.rightViewMode = UITextFieldViewModeAlways;
return textField;
}