forked from trentmilton/SDF-NSTextField-Validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSTextField+Validation.m
166 lines (133 loc) · 4.67 KB
/
NSTextField+Validation.m
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// NSTextField+Validation.m
//
// Created by Trent Milton on 02/07/2014.
// Copyright (c) 2014 shaydes.dsgn. All rights reserved.
//
#import "NSTextField+Validation.h"
#import "NSString+Validate.h"
#import <objc/runtime.h>
static NSColor *kValidationTextFieldBackgroundColour;
static NSColor *kValidationTextFieldValidBackgroundColour;
static NSColor *kValidationTextFieldInvalidBackgroundColour;
static NSColor *kValidationTextFieldPlaceholderValidateFontColour;
static NSColor *kValidationTextFieldPlaceholderFontColour;
static void *TextFieldValidationTypePropertyKey = &TextFieldValidationTypePropertyKey;
static void *PlaceholderStringPropertyKey = &PlaceholderStringPropertyKey;
@implementation NSTextField (Validation)
+ (void)initialize
{
if (self == [NSTextField class]) {
kValidationTextFieldBackgroundColour = [NSColor whiteColor];
kValidationTextFieldValidBackgroundColour = [NSColor greenColor];
kValidationTextFieldInvalidBackgroundColour = [NSColor redColor];
kValidationTextFieldPlaceholderValidateFontColour = [NSColor whiteColor];
kValidationTextFieldPlaceholderFontColour = [NSColor grayColor];
}
}
#pragma mark - Properties
- (TextFieldValidationType)textFieldValidationType
{
NSNumber *t = objc_getAssociatedObject(self, TextFieldValidationTypePropertyKey);
return t.intValue;
}
- (void)setTextFieldValidationType:(TextFieldValidationType)textFieldValidationType
{
objc_setAssociatedObject(self, TextFieldValidationTypePropertyKey, @(textFieldValidationType), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)placeholderString
{
return objc_getAssociatedObject(self, PlaceholderStringPropertyKey);
}
- (void)setPlaceholderString:(NSString *)placeholderString
{
objc_setAssociatedObject(self, PlaceholderStringPropertyKey, placeholderString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - Public
- (BOOL)validate
{
NSColor *bgColour = kValidationTextFieldBackgroundColour;
NSColor *placeholderColour = kValidationTextFieldPlaceholderValidateFontColour;
BOOL valid = YES;
switch (self.textFieldValidationType) {
case TextFieldValidationTypeNone:
// Do nothing, we just want white
placeholderColour = kValidationTextFieldPlaceholderFontColour;
break;
case TextFieldValidationTypeEmailOptional:
// IMPORTANT: make sure this is above the ValidationTypeEmail (which should be below)
if (self.stringValue.length == 0) {
// Do nothing, we just want white
placeholderColour = kValidationTextFieldPlaceholderFontColour;
break;
}
// Else: Do nothing, flow threw to ValidationTypeEmail
case TextFieldValidationTypeEmail: {
valid = [self.stringValue validEmail];
bgColour = valid ? kValidationTextFieldValidBackgroundColour : kValidationTextFieldInvalidBackgroundColour;
break;
}
case TextFieldValidationTypeOptional:
placeholderColour = kValidationTextFieldPlaceholderFontColour;
bgColour = kValidationTextFieldBackgroundColour;
break;
case TextFieldValidationTypeRequired:
case TextFieldValidationTypePassword:
default: {
valid = self.stringValue.length > 0;
bgColour = valid ? kValidationTextFieldValidBackgroundColour : kValidationTextFieldInvalidBackgroundColour;
break;
}
}
[self setPlaceholderFontColour:placeholderColour];
self.backgroundColor = bgColour;
// This will make sure the background updates
[self setEditable:NO];
[self setEditable:YES];
return valid;
}
- (void)reset
{
self.stringValue = @"";
self.backgroundColor = kValidationTextFieldBackgroundColour;
[self setPlaceholderFontColour:kValidationTextFieldPlaceholderFontColour];
// This will make sure the background updates
[self setEditable:NO];
[self setEditable:YES];
}
#pragma mark - Private
- (void)setPlaceholderFontColour:(NSColor *)colour
{
if (!self.placeholderString) {
self.placeholderString = [self.cell placeholderString];
}
NSDictionary *d = @{
NSFontAttributeName : self.font,
NSForegroundColorAttributeName : colour
};
NSAttributedString *as = [[NSAttributedString alloc] initWithString:self.placeholderString
attributes:d];
[self.cell setPlaceholderAttributedString:as];
}
#pragma mark - Static Properties
+ (void)setBackgroundColour:(NSColor *)colour
{
kValidationTextFieldBackgroundColour = colour;
}
+ (void)setValidBackgroundColour:(NSColor *)colour
{
kValidationTextFieldValidBackgroundColour = colour;
}
+ (void)setInvalidBackgroundColour:(NSColor *)colour
{
kValidationTextFieldInvalidBackgroundColour = colour;
}
+ (void)setPlaceholderValidateFontColour:(NSColor *)colour
{
kValidationTextFieldPlaceholderValidateFontColour = colour;
}
+ (void)setPlaceholderFontColour:(NSColor *)colour
{
kValidationTextFieldPlaceholderFontColour = colour;
}
@end