UITextField密码输入切换明暗文

2016-08-19  本文已影响1870人  Louis_hey

本文提供的是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;
}
上一篇下一篇

猜你喜欢

热点阅读