From a5a05bc54da40d4e1c6cfaece3464c8b387dce68 Mon Sep 17 00:00:00 2001 From: Ayaka Nonaka Date: Thu, 13 Feb 2014 10:57:33 -0800 Subject: [PATCH] Add style customizability to VENCalculatorInputView. --- .../VENCalculatorInputView.h | 17 ++++ .../VENCalculatorInputView.m | 82 ++++++++++++++++--- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/VENCalculatorInputView/VENCalculatorInputView.h b/VENCalculatorInputView/VENCalculatorInputView.h index 0c68026..1b88df7 100644 --- a/VENCalculatorInputView/VENCalculatorInputView.h +++ b/VENCalculatorInputView/VENCalculatorInputView.h @@ -12,7 +12,24 @@ @interface VENCalculatorInputView : UIView @property (weak, nonatomic) id delegate; + @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *numberButtonCollection; @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *operationButtonCollection; + +/**----------------------------------------------------------------------------- + * @name Customizing colors + * ----------------------------------------------------------------------------- + */ + +@property (strong, nonatomic) UIColor *buttonTitleColor; +@property (strong, nonatomic) UIFont *buttonTitleFont; +@property (strong, nonatomic) UIColor *buttonHighlightedColor; + +@property (strong, nonatomic) UIColor *numberButtonBackgroundColor; +@property (strong, nonatomic) UIColor *numberButtonBorderColor; + +@property (strong, nonatomic) UIColor *operationButtonBackgroundColor; +@property (strong, nonatomic) UIColor *operationButtonBorderColor; + @end diff --git a/VENCalculatorInputView/VENCalculatorInputView.m b/VENCalculatorInputView/VENCalculatorInputView.m index ff40922..beacc76 100644 --- a/VENCalculatorInputView/VENCalculatorInputView.m +++ b/VENCalculatorInputView/VENCalculatorInputView.m @@ -5,27 +5,26 @@ @implementation VENCalculatorInputView - (id)initWithFrame:(CGRect)frame { self = [[[NSBundle mainBundle] loadNibNamed:@"VENCalculatorInputView" owner:self options:nil] firstObject]; if (self) { + // Set customizable properties + [self setNumberButtonBackgroundColor:[UIColor colorWithWhite:0.98828 alpha:1]]; + [self setNumberButtonBorderColor:[UIColor colorWithRed:193/255.0f green:195/255.0f blue:199/255.0f alpha:1]]; + [self setOperationButtonBackgroundColor:[UIColor colorWithRed:193/255.0f green:196/255.0f blue:200/255.0f alpha:1]]; + [self setOperationButtonBorderColor:[UIColor colorWithRed:172/255.0f green:174/255.0f blue:177/255.0f alpha:1]]; + [self setButtonHighlightedColor:[UIColor grayColor]]; + [self setButtonTitleColor:[UIColor darkTextColor]]; + + // Set default properties for (UIButton *numberButton in self.numberButtonCollection) { [self setupButton:numberButton]; - [numberButton setBackgroundColor:[UIColor colorWithWhite:0.98828 alpha:1]]; - numberButton.layer.borderColor = [UIColor colorWithRed:193/255.0f green:195/255.0f blue:199/255.0f alpha:1].CGColor; } for (UIButton *operationButton in self.operationButtonCollection) { [self setupButton:operationButton]; - [operationButton setBackgroundColor:[UIColor colorWithRed:193/255.0f green:196/255.0f blue:200/255.0f alpha:1]]; - operationButton.layer.borderColor = [UIColor colorWithRed:172/255.0f green:174/255.0f blue:177/255.0f alpha:1].CGColor; } } return self; } - (void)setupButton:(UIButton *)button { - button.adjustsImageWhenHighlighted = YES; - [button setBackgroundImage:nil forState:UIControlStateNormal]; - [button setBackgroundImage:nil forState:UIControlStateHighlighted]; - [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; - [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; - [button setBackgroundImage:[self imageWithColor:[UIColor grayColor] size:CGSizeMake(50, 50)] forState:UIControlStateHighlighted]; button.layer.borderWidth = 0.25f; } @@ -65,4 +64,67 @@ - (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size { return image; } + +#pragma mark - Properties + +- (void)setButtonTitleColor:(UIColor *)buttonTitleColor { + _buttonTitleColor = buttonTitleColor; + for (UIButton *numberButton in self.numberButtonCollection) { + [numberButton setTitleColor:buttonTitleColor forState:UIControlStateNormal]; + } + for (UIButton *operationButton in self.operationButtonCollection) { + [operationButton setTitleColor:buttonTitleColor forState:UIControlStateNormal]; + } +} + +- (void)setButtonTitleFont:(UIFont *)buttonTitleFont { + _buttonTitleFont = buttonTitleFont; + for (UIButton *numberButton in self.numberButtonCollection) { + numberButton.titleLabel.font = buttonTitleFont; + } + for (UIButton *operationButton in self.operationButtonCollection) { + operationButton.titleLabel.font = buttonTitleFont; + } +} + +- (void)setButtonHighlightedColor:(UIColor *)buttonHighlightedColor { + _buttonHighlightedColor = buttonHighlightedColor; + for (UIButton *numberButton in self.numberButtonCollection) { + [numberButton setBackgroundImage:[self imageWithColor:buttonHighlightedColor size:CGSizeMake(50, 50)] + forState:UIControlStateHighlighted]; + } + for (UIButton *operationButton in self.operationButtonCollection) { + [operationButton setBackgroundImage:[self imageWithColor:buttonHighlightedColor size:CGSizeMake(50, 50)] + forState:UIControlStateHighlighted]; + } +} + +- (void)setNumberButtonBackgroundColor:(UIColor *)numberButtonBackgroundColor { + _numberButtonBackgroundColor = numberButtonBackgroundColor; + for (UIButton *numberButton in self.numberButtonCollection) { + numberButton.backgroundColor = numberButtonBackgroundColor; + } +} + +- (void)setNumberButtonBorderColor:(UIColor *)numberButtonBorderColor { + _numberButtonBorderColor = numberButtonBorderColor; + for (UIButton *numberButton in self.numberButtonCollection) { + numberButton.layer.borderColor = numberButtonBorderColor.CGColor; + } +} + +- (void)setOperationButtonBackgroundColor:(UIColor *)operationButtonBackgroundColor { + _operationButtonBackgroundColor = operationButtonBackgroundColor; + for (UIButton *operationButton in self.operationButtonCollection) { + operationButton.backgroundColor = operationButtonBackgroundColor; + } +} + +- (void)setOperationButtonBorderColor:(UIColor *)operationButtonBorderColor { + _operationButtonBorderColor = operationButtonBorderColor; + for (UIButton *operationButton in self.operationButtonCollection) { + operationButton.layer.borderColor = operationButtonBorderColor.CGColor; + } +} + @end