JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
//  Copyright © 2016年 dahua. All rights reserved.
//
 
#import <LCBaseModule/LCInputView.h>
#import <LCBaseModule/LCTextField.h>
#import <LCBaseModule/UITextField+LeChange.h>
#import <LCBaseModule/UIColor+LeChange.h>
#import <LCBaseModule/UIFont+Dahua.h>
 
/// 右侧按钮尺寸
#define kRightItemSize 25
 
/// 右侧按钮边距
#define kRightPadding  10
 
/// 左侧按钮边距
#define kLeftPadding   15
 
@implementation LCInputView
 
- (void)awakeFromNib {
    [super awakeFromNib];
}
 
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
 
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}
 
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), self.frame.size.height);
    if (_rightBtn.isHidden == false && _btnDirection == LCInputViewBtnDirectionLeft) {
        frame.origin.x =  kLeftPadding + kRightItemSize + 5;
        frame.size.width = frame.size.width - kRightItemSize - kLeftPadding - 5;
        _rightBtn.frame = CGRectMake(kLeftPadding,
                                     (CGRectGetHeight(self.bounds) - kRightItemSize) / 2,
                                     kRightItemSize,
                                     kRightItemSize);
    }
    if (_rightBtn.isHidden == false && _btnDirection == LCInputViewBtnDirectionRight) {
        frame.origin.x = 0;
        frame.size.width = frame.size.width - kRightItemSize - kRightPadding - 5;
        _rightBtn.frame = CGRectMake(frame.size.width + 5,
                                     (CGRectGetHeight(self.bounds) - kRightItemSize) / 2,
                                     kRightItemSize,
                                     kRightItemSize);
    }
 
    _textField.frame = frame;
}
 
- (void)setSwitchBtnHidden:(BOOL)isHidden
{
    _rightBtn.hidden = isHidden;
    [self setNeedsLayout];
}
 
- (void)setup
{
    self.backgroundColor = [UIColor dhcolor_c43];
    _textField = [[LCTextField alloc] init];
    _textField.backgroundColor = [UIColor clearColor];
    //不要在通用控件中修改默认大小和对齐模式,影响其他界面的展示
    _textField.textAlignment = NSTextAlignmentLeft;
    //_textField.font = [UIFont dhFont_t2];
    _textField.keyboardType = UIKeyboardTypeASCIICapable;
    _textField.customClearButton = YES;
    _textField.secureTextEntry = YES;
    [self addSubview:_textField];
 
    _rightBtn = [[UIButton alloc]init];
    self.openBtnState = NO;
    [_rightBtn addTarget:self action:@selector(hitClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rightBtn];
}
 
- (void)hitClick
{
    BOOL bShowState = ![_textField isSecureTextEntry];//获取状态
    [_textField setSecureTextEntry:bShowState];//设置状态
    [_textField setEnabled:YES];
    if (bShowState) {
        [_rightBtn setImage:[UIImage imageNamed:@"login_icon_closeeye"] forState:UIControlStateNormal];
        [_rightBtn setImage:[UIImage imageNamed:@"login_icon_closeeye_click"] forState:UIControlStateSelected];
    } else {
        [_rightBtn setImage:[UIImage imageNamed:@"login_icon_openeye"] forState:UIControlStateNormal];
        [_rightBtn setImage:[UIImage imageNamed:@"login_icon_openeye_click"] forState:UIControlStateSelected];
    }
}
 
- (void)setOpenBtnState:(BOOL)openBtnState
{
    _textField.secureTextEntry = !openBtnState;
    UIImage *imageNormal = openBtnState ? [UIImage imageNamed:@"login_icon_openeye"] : [UIImage imageNamed:@"login_icon_closeeye"];
    UIImage *imageSelected = openBtnState ? [UIImage imageNamed:@"login_icon_openeye_click"] : [UIImage imageNamed:@"login_icon_closeeye_click"];
    [_rightBtn setImage:imageNormal forState:UIControlStateNormal];
    [_rightBtn setImage:imageSelected forState:UIControlStateSelected];
}
 
- (void)setSwitchEnable:(BOOL)switchEnable
{
    _switchEnable = switchEnable;
    _rightBtn.enabled = switchEnable;
    if (!switchEnable) {
        self.openBtnState = NO;
    }
}
 
@end