Skip to content

Commit 9848de9

Browse files
committed
Merge pull request Sumi-Interactive#18 from reekris/button-colors
add support for customizing button text colors
2 parents f6b3a0a + ae84272 commit 9848de9

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

SIAlertView/SIAlertView.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
5656
@property (nonatomic, strong) UIFont *titleFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
5757
@property (nonatomic, strong) UIFont *messageFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
5858
@property (nonatomic, strong) UIFont *buttonFont NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
59+
@property (nonatomic, strong) UIColor *buttonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
60+
@property (nonatomic, strong) UIColor *cancelButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
61+
@property (nonatomic, strong) UIColor *destructiveButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
5962
@property (nonatomic, assign) CGFloat cornerRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 2.0
6063
@property (nonatomic, assign) CGFloat shadowRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 8.0
6164

SIAlertView/SIAlertView.m

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ + (void)initialize
197197
appearance.titleFont = [UIFont boldSystemFontOfSize:20];
198198
appearance.messageFont = [UIFont systemFontOfSize:16];
199199
appearance.buttonFont = [UIFont systemFontOfSize:[UIFont buttonFontSize]];
200+
appearance.buttonColor = [UIColor colorWithWhite:0.4 alpha:1];
201+
appearance.cancelButtonColor = [UIColor colorWithWhite:0.3 alpha:1];
202+
appearance.destructiveButtonColor = [UIColor whiteColor];
200203
appearance.cornerRadius = 2;
201204
appearance.shadowRadius = 8;
202205
}
@@ -865,21 +868,21 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
865868
case SIAlertViewButtonTypeCancel:
866869
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-cancel"];
867870
highlightedImage = [UIImage imageNamed:@"SIAlertView.bundle/button-cancel-d"];
868-
[button setTitleColor:[UIColor colorWithWhite:0.3 alpha:1] forState:UIControlStateNormal];
869-
[button setTitleColor:[UIColor colorWithWhite:0.3 alpha:0.8] forState:UIControlStateHighlighted];
871+
[button setTitleColor:self.cancelButtonColor forState:UIControlStateNormal];
872+
[button setTitleColor:[self.cancelButtonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
870873
break;
871874
case SIAlertViewButtonTypeDestructive:
872875
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-destructive"];
873876
highlightedImage = [UIImage imageNamed:@"SIAlertView.bundle/button-destructive-d"];
874-
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
875-
[button setTitleColor:[UIColor colorWithWhite:1 alpha:0.8] forState:UIControlStateHighlighted];
877+
[button setTitleColor:self.destructiveButtonColor forState:UIControlStateNormal];
878+
[button setTitleColor:[self.destructiveButtonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
876879
break;
877880
case SIAlertViewButtonTypeDefault:
878881
default:
879882
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-default"];
880883
highlightedImage = [UIImage imageNamed:@"SIAlertView.bundle/button-default-d"];
881-
[button setTitleColor:[UIColor colorWithWhite:0.4 alpha:1] forState:UIControlStateNormal];
882-
[button setTitleColor:[UIColor colorWithWhite:0.4 alpha:0.8] forState:UIControlStateHighlighted];
884+
[button setTitleColor:self.buttonColor forState:UIControlStateNormal];
885+
[button setTitleColor:[self.buttonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
883886
break;
884887
}
885888
CGFloat hInset = floorf(normalImage.size.width / 2);
@@ -994,4 +997,42 @@ - (void)setShadowRadius:(CGFloat)shadowRadius
994997
self.containerView.layer.shadowRadius = shadowRadius;
995998
}
996999

1000+
- (void)setButtonColor:(UIColor *)buttonColor
1001+
{
1002+
if (_buttonColor == buttonColor) {
1003+
return;
1004+
}
1005+
_buttonColor = buttonColor;
1006+
[self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeDefault];
1007+
}
1008+
1009+
- (void)setCancelButtonColor:(UIColor *)buttonColor
1010+
{
1011+
if (_cancelButtonColor == buttonColor) {
1012+
return;
1013+
}
1014+
_cancelButtonColor = buttonColor;
1015+
[self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeCancel];
1016+
}
1017+
1018+
- (void)setDestructiveButtonColor:(UIColor *)buttonColor
1019+
{
1020+
if (_destructiveButtonColor == buttonColor) {
1021+
return;
1022+
}
1023+
_destructiveButtonColor = buttonColor;
1024+
[self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeDestructive];
1025+
}
1026+
1027+
-(void)setColor:(UIColor *)color toButtonsOfType:(SIAlertViewButtonType)type {
1028+
for (NSUInteger i = 0; i < self.items.count; i++) {
1029+
SIAlertItem *item = self.items[i];
1030+
if(item.type == type) {
1031+
UIButton *button = self.buttons[i];
1032+
[button setTitleColor:color forState:UIControlStateNormal];
1033+
[button setTitleColor:[color colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
1034+
}
1035+
}
1036+
}
1037+
9971038
@end

SIAlertViewExample/SIAlertViewExample/ViewController.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ - (void)viewDidLoad
2727
[[SIAlertView appearance] setCornerRadius:12];
2828
[[SIAlertView appearance] setShadowRadius:20];
2929
[[SIAlertView appearance] setViewBackgroundColor:[UIColor colorWithRed:0.891 green:0.936 blue:0.978 alpha:1.000]];
30+
[[SIAlertView appearance] setButtonColor:[UIColor greenColor]];
31+
[[SIAlertView appearance] setCancelButtonColor:[UIColor redColor]];
32+
[[SIAlertView appearance] setDestructiveButtonColor:[UIColor blueColor]];
3033
#endif
3134
}
3235

0 commit comments

Comments
 (0)