Skip to content

Commit 368a09a

Browse files
committed
add support for customizing button text colors
1 parent 8465e11 commit 368a09a

3 files changed

Lines changed: 52 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: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ + (void)initialize
181181
appearance.titleFont = [UIFont boldSystemFontOfSize:20];
182182
appearance.messageFont = [UIFont systemFontOfSize:16];
183183
appearance.buttonFont = [UIFont systemFontOfSize:[UIFont buttonFontSize]];
184+
appearance.buttonColor = [UIColor colorWithWhite:0.4 alpha:1];
185+
appearance.cancelButtonColor = [UIColor colorWithWhite:0.3 alpha:1];
186+
appearance.destructiveButtonColor = [UIColor whiteColor];
184187
appearance.cornerRadius = 2;
185188
appearance.shadowRadius = 8;
186189
}
@@ -843,21 +846,21 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
843846
case SIAlertViewButtonTypeCancel:
844847
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-cancel"];
845848
highlightedImage = [UIImage imageNamed:@"SIAlertView.bundle/button-cancel-d"];
846-
[button setTitleColor:[UIColor colorWithWhite:0.3 alpha:1] forState:UIControlStateNormal];
847-
[button setTitleColor:[UIColor colorWithWhite:0.3 alpha:0.8] forState:UIControlStateHighlighted];
849+
[button setTitleColor:self.cancelButtonColor forState:UIControlStateNormal];
850+
[button setTitleColor:[self.cancelButtonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
848851
break;
849852
case SIAlertViewButtonTypeDestructive:
850853
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-destructive"];
851854
highlightedImage = [UIImage imageNamed:@"SIAlertView.bundle/button-destructive-d"];
852-
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
853-
[button setTitleColor:[UIColor colorWithWhite:1 alpha:0.8] forState:UIControlStateHighlighted];
855+
[button setTitleColor:self.destructiveButtonColor forState:UIControlStateNormal];
856+
[button setTitleColor:[self.destructiveButtonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
854857
break;
855858
case SIAlertViewButtonTypeDefault:
856859
default:
857860
normalImage = [UIImage imageNamed:@"SIAlertView.bundle/button-default"];
858861
highlightedImage = [UIImage imageNamed:@"SIAlertView.bundle/button-default-d"];
859-
[button setTitleColor:[UIColor colorWithWhite:0.4 alpha:1] forState:UIControlStateNormal];
860-
[button setTitleColor:[UIColor colorWithWhite:0.4 alpha:0.8] forState:UIControlStateHighlighted];
862+
[button setTitleColor:self.buttonColor forState:UIControlStateNormal];
863+
[button setTitleColor:[self.buttonColor colorWithAlphaComponent:0.8] forState:UIControlStateHighlighted];
861864
break;
862865
}
863866
CGFloat hInset = floorf(normalImage.size.width / 2);
@@ -972,4 +975,41 @@ - (void)setShadowRadius:(CGFloat)shadowRadius
972975
self.containerView.layer.shadowRadius = shadowRadius;
973976
}
974977

978+
- (void)setButtonColor:(UIColor *)buttonColor
979+
{
980+
if (_buttonColor == buttonColor) {
981+
return;
982+
}
983+
_buttonColor = buttonColor;
984+
[self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeDefault];
985+
}
986+
987+
- (void)setCancelButtonColor:(UIColor *)buttonColor
988+
{
989+
if (_cancelButtonColor == buttonColor) {
990+
return;
991+
}
992+
_cancelButtonColor = buttonColor;
993+
[self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeCancel];
994+
}
995+
996+
- (void)setDestructiveButtonColor:(UIColor *)buttonColor
997+
{
998+
if (_destructiveButtonColor == buttonColor) {
999+
return;
1000+
}
1001+
_destructiveButtonColor = buttonColor;
1002+
[self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeDestructive];
1003+
}
1004+
1005+
-(void)setColor:(UIColor *)color toButtonsOfType:(SIAlertViewButtonType)type {
1006+
for (NSUInteger i = 0; i < self.items.count; i++) {
1007+
SIAlertItem *item = self.items[i];
1008+
if(item.type == type) {
1009+
UIButton *button = self.buttons[i];
1010+
[button setTitleColor:color forState:UIControlStateNormal];
1011+
}
1012+
}
1013+
}
1014+
9751015
@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)