Skip to content

Commit 9752ecc

Browse files
committed
Merge pull request #8 from khaullen/master
Modifies CYRKeyboardButton to allow initialization from interface builder
2 parents 0ad5bf0 + da8a57b commit 9752ecc

File tree

1 file changed

+58
-44
lines changed

1 file changed

+58
-44
lines changed

CYRKeyboardButton/CYRKeyboardButton.m

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,55 +64,69 @@ - (id)initWithFrame:(CGRect)frame
6464
{
6565
self = [super initWithFrame:frame];
6666
if (self) {
67-
switch ([UIDevice currentDevice].userInterfaceIdiom) {
68-
case UIUserInterfaceIdiomPhone:
69-
_style = CYRKeyboardButtonStylePhone;
70-
break;
71-
72-
case UIUserInterfaceIdiomPad:
73-
_style = CYRKeyboardButtonStyleTablet;
74-
break;
75-
76-
default:
77-
break;
78-
}
79-
80-
// Default appearance
81-
_font = [UIFont systemFontOfSize:22.f];
82-
_inputOptionsFont = [UIFont systemFontOfSize:24.f];
83-
_keyColor = [UIColor whiteColor];
84-
_keyTextColor = [UIColor blackColor];
85-
_keyShadowColor = [UIColor colorWithRed:136 / 255.f green:138 / 255.f blue:142 / 255.f alpha:1];
86-
_keyHighlightedColor = [UIColor colorWithRed:213/255.f green:214/255.f blue:216/255.f alpha:1];
87-
88-
// Styling
89-
self.backgroundColor = [UIColor clearColor];
90-
self.clipsToBounds = NO;
91-
self.layer.masksToBounds = NO;
92-
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
93-
94-
// State handling
95-
[self addTarget:self action:@selector(handleTouchDown) forControlEvents:UIControlEventTouchDown];
96-
[self addTarget:self action:@selector(handleTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
97-
98-
// Input label
99-
UILabel *inputLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
100-
inputLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
101-
inputLabel.textAlignment = NSTextAlignmentCenter;
102-
inputLabel.backgroundColor = [UIColor clearColor];
103-
inputLabel.userInteractionEnabled = NO;
104-
inputLabel.textColor = _keyTextColor;
105-
inputLabel.font = _font;
106-
107-
[self addSubview:inputLabel];
108-
_inputLabel = inputLabel;
109-
110-
[self updateDisplayStyle];
67+
[self commonInit];
11168
}
11269

11370
return self;
11471
}
11572

73+
- (instancetype)initWithCoder:(NSCoder *)aDecoder
74+
{
75+
self = [super initWithCoder:aDecoder];
76+
if (self) {
77+
[self commonInit];
78+
}
79+
return self;
80+
}
81+
82+
- (void)commonInit
83+
{
84+
switch ([UIDevice currentDevice].userInterfaceIdiom) {
85+
case UIUserInterfaceIdiomPhone:
86+
_style = CYRKeyboardButtonStylePhone;
87+
break;
88+
89+
case UIUserInterfaceIdiomPad:
90+
_style = CYRKeyboardButtonStyleTablet;
91+
break;
92+
93+
default:
94+
break;
95+
}
96+
97+
// Default appearance
98+
_font = [UIFont systemFontOfSize:22.f];
99+
_inputOptionsFont = [UIFont systemFontOfSize:24.f];
100+
_keyColor = [UIColor whiteColor];
101+
_keyTextColor = [UIColor blackColor];
102+
_keyShadowColor = [UIColor colorWithRed:136 / 255.f green:138 / 255.f blue:142 / 255.f alpha:1];
103+
_keyHighlightedColor = [UIColor colorWithRed:213/255.f green:214/255.f blue:216/255.f alpha:1];
104+
105+
// Styling
106+
self.backgroundColor = [UIColor clearColor];
107+
self.clipsToBounds = NO;
108+
self.layer.masksToBounds = NO;
109+
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
110+
111+
// State handling
112+
[self addTarget:self action:@selector(handleTouchDown) forControlEvents:UIControlEventTouchDown];
113+
[self addTarget:self action:@selector(handleTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
114+
115+
// Input label
116+
UILabel *inputLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
117+
inputLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
118+
inputLabel.textAlignment = NSTextAlignmentCenter;
119+
inputLabel.backgroundColor = [UIColor clearColor];
120+
inputLabel.userInteractionEnabled = NO;
121+
inputLabel.textColor = _keyTextColor;
122+
inputLabel.font = _font;
123+
124+
[self addSubview:inputLabel];
125+
_inputLabel = inputLabel;
126+
127+
[self updateDisplayStyle];
128+
}
129+
116130
- (void)didMoveToSuperview
117131
{
118132
[self updateButtonPosition];

0 commit comments

Comments
 (0)