UITextField密码输入切换明暗文

本文提供的是UITextField切换密码输入明暗文的一个分类,拿到项目中可以直接使用

.h文件

@interface UITextField (Extention)
//设置密码输入框
- (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;
}
//监听右边按钮的点击,切换密码输入明暗文状态
-(void)btnClick:(UIButton *)btn{
    //解决明暗文切换后面空格的问题的两种方式
    //NSString* text = self.text;
    //self.text = @" ";
    //self.text = text;
    //[self becomeFirstResponder];
    [self resignFirstResponder];//取消第一响应者
    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];//放弃第一响应者
}

@end

效果图


test.gif

ps:UITextField其他常用的分类类方法

// 设置可以带背景图片的文本框
+ (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;
}

// 设置右边可以带图片的文本框
+ (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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容