28
28
#import " OTPToken+Generation.h"
29
29
30
30
31
- @interface OTPTokenCell () <UITextFieldDelegate>
31
+ @interface OTPTokenCell ()
32
32
33
- @property (nonatomic , strong ) UITextField *nameLabel;
34
- @property (nonatomic , strong ) UITextField *issuerLabel;
33
+ @property (nonatomic , strong ) UILabel *titleLabel;
35
34
@property (nonatomic , strong ) UILabel *passwordLabel;
36
35
@property (nonatomic , strong ) UIButton *nextPasswordButton;
37
36
@@ -75,32 +74,23 @@ - (void)createSubviews
75
74
{
76
75
self.backgroundColor = [UIColor otpBackgroundColor ];
77
76
78
- self.nameLabel = [UITextField new ];
79
- self.nameLabel .font = [UIFont fontWithName: @" HelveticaNeue-Light" size: 15 ];
80
- self.nameLabel .textColor = [UIColor otpForegroundColor ];
81
- self.nameLabel .returnKeyType = UIReturnKeyDone;
82
- self.nameLabel .delegate = self;
83
- self.nameLabel .enabled = NO ;
84
-
85
- self.issuerLabel = [UITextField new ];
86
- self.issuerLabel .font = [UIFont fontWithName: @" HelveticaNeue-Medium" size: 15 ];
87
- self.issuerLabel .textColor = [UIColor otpForegroundColor ];
88
- self.issuerLabel .returnKeyType = UIReturnKeyNext;
89
- self.issuerLabel .delegate = self;
90
- self.issuerLabel .enabled = NO ;
77
+ self.titleLabel = [UILabel new ];
78
+ self.titleLabel .font = [UIFont fontWithName: @" HelveticaNeue-Light" size: 15 ];
79
+ self.titleLabel .textColor = [UIColor otpForegroundColor ];
80
+ self.titleLabel .textAlignment = NSTextAlignmentCenter;
91
81
92
82
self.passwordLabel = [UILabel new ];
93
83
self.passwordLabel .font = [UIFont fontWithName: @" HelveticaNeue-UltraLight" size: 50 ];
94
84
self.passwordLabel .textColor = [UIColor otpForegroundColor ];
85
+ self.passwordLabel .textAlignment = NSTextAlignmentCenter;
95
86
96
- [self .contentView addSubview: self .nameLabel];
97
- [self .contentView addSubview: self .issuerLabel];
87
+ [self .contentView addSubview: self .titleLabel];
98
88
[self .contentView addSubview: self .passwordLabel];
99
89
100
90
self.nextPasswordButton = [UIButton buttonWithType: UIButtonTypeContactAdd];
101
91
self.nextPasswordButton .tintColor = [UIColor otpForegroundColor ];
102
92
[self .nextPasswordButton addTarget: self action: @selector (generateNextPassword ) forControlEvents: UIControlEventTouchUpInside];
103
- self.accessoryView = self.nextPasswordButton ;
93
+ [ self .contentView addSubview: self .nextPasswordButton] ;
104
94
}
105
95
106
96
- (void )generateNextPassword
@@ -112,24 +102,20 @@ - (void)layoutSubviews
112
102
{
113
103
[super layoutSubviews ];
114
104
115
- CGRect insetFrame = CGRectInset (self.contentView .bounds , 10 , 5 );
105
+ CGRect insetFrame = [self convertRect: self .bounds toView: self .contentView];
106
+ insetFrame.origin .x = MIN (insetFrame.origin .x , self.contentView .bounds .origin .x );
116
107
117
108
CGRect frame = insetFrame;
118
109
frame.size .height = 20 ;
119
- frame.size .width = (self.issuerLabel .text .length || self.editing ) ? [self .issuerLabel sizeThatFits: frame.size].width : 0 ;
120
- if (self.editing && frame.size .width < 80 ) {
121
- frame.size .width = 80 ;
122
- }
123
- self.issuerLabel .frame = frame;
124
-
125
- frame.origin .x += frame.size .width ;
126
- frame.size .width = CGRectGetMaxX (insetFrame) - frame.origin .x ;
127
- self.nameLabel .frame = frame;
110
+ self.titleLabel .frame = frame;
128
111
129
112
frame = insetFrame;
130
113
frame.origin .y += 20 ;
131
- frame.size .height -= 20 ;
114
+ frame.size .height -= 30 ;
132
115
self.passwordLabel .frame = frame;
116
+
117
+ self.nextPasswordButton .center = CGPointMake (CGRectGetMaxX (insetFrame) - 25 ,
118
+ CGRectGetMidY (self.passwordLabel .frame ));
133
119
}
134
120
135
121
@@ -161,10 +147,18 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
161
147
162
148
- (void )refresh
163
149
{
164
- if (!self.editing ) {
165
- self.nameLabel .text = self.token .name ;
166
- self.issuerLabel .text = self.token .issuer ;
150
+ NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc ] init ];
151
+ if (self.token .issuer .length ) {
152
+ [titleString appendAttributedString: [[NSAttributedString alloc ] initWithString: self .token.issuer attributes: @{NSFontAttributeName : [UIFont fontWithName: @" HelveticaNeue-Medium" size: 15 ]}]];
153
+ }
154
+ if (self.token .issuer .length && self.token .name .length ) {
155
+ [titleString appendAttributedString: [[NSAttributedString alloc ] initWithString: @" " ]];
156
+ }
157
+ if (self.token .name .length ) {
158
+ [titleString appendAttributedString: [[NSAttributedString alloc ] initWithString: self .token.name]];
167
159
}
160
+ self.titleLabel .attributedText = titleString;
161
+
168
162
self.passwordLabel .text = self.token .password ;
169
163
self.nextPasswordButton .hidden = self.token .type != OTPTokenTypeCounter;
170
164
}
@@ -176,50 +170,10 @@ - (void)setEditing:(BOOL)editing animated:(BOOL)animated
176
170
{
177
171
[super setEditing: editing animated: animated];
178
172
179
- self.nameLabel .enabled = editing;
180
- self.issuerLabel .enabled = self.nameLabel .enabled ;
181
-
182
- NSAttributedString *namePlaceholder, *issuerPlaceholder;
183
- if (editing) {
184
- UIColor *placeholderColor = [[UIColor otpForegroundColor ] colorWithAlphaComponent: (CGFloat)0.3 ];
185
- namePlaceholder = [[NSAttributedString alloc ] initWithString: @" Name"
186
- attributes: @{NSForegroundColorAttributeName : placeholderColor}];
187
- issuerPlaceholder = [[NSAttributedString alloc ] initWithString: @" Issuer"
188
- attributes: @{NSForegroundColorAttributeName : placeholderColor}];
189
- }
190
- self.nameLabel .attributedPlaceholder = namePlaceholder;
191
- self.issuerLabel .attributedPlaceholder = issuerPlaceholder;
192
-
193
173
[UIView animateWithDuration: 0.3 animations: ^{
194
174
self.passwordLabel .alpha = !editing ? 1 : (CGFloat)0.2 ;
175
+ self.nextPasswordButton .alpha = !editing ? 1 : 0 ;
195
176
}];
196
-
197
- if (!editing) {
198
- [self .nameLabel resignFirstResponder ];
199
- [self .issuerLabel resignFirstResponder ];
200
-
201
- [self commitChanges ];
202
- }
203
- }
204
-
205
- - (void )commitChanges
206
- {
207
- if (![self .token.name isEqualToString: self .nameLabel.text] ||
208
- ![self .token.issuer isEqualToString: self .issuerLabel.text]) {
209
- self.token .name = self.nameLabel .text ;
210
- self.token .issuer = self.issuerLabel .text ;
211
- [self .token saveToKeychain ];
212
- }
213
- }
214
-
215
- - (BOOL )textFieldShouldReturn : (UITextField *)textField
216
- {
217
- if (textField == self.issuerLabel ) {
218
- [self .nameLabel becomeFirstResponder ];
219
- } else {
220
- [textField resignFirstResponder ];
221
- }
222
- return NO ;
223
177
}
224
178
225
179
@end
0 commit comments