Skip to content

Commit 01e7fbb

Browse files
committed
add convenience init api;
remove tint support
1 parent b23cdef commit 01e7fbb

2 files changed

Lines changed: 55 additions & 38 deletions

File tree

SIAlertView/SIAlertView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
7878

7979

8080
- (id)initWithTitle:(NSString *)title message:(NSString *)message;
81+
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButton:(NSString *)canelButton handler:(SIAlertViewHandler)handler;
8182
- (id)initWithAttributedTitle:(NSAttributedString *)attributedTitle attributedMessage:(NSAttributedString *)attributedMessage;
8283
- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler;
84+
- (void)addButtonWithTitle:(NSString *)title font:(UIFont *)font color:(UIColor *)color type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler;
8385
- (void)addButtonWithAttributedTitle:(NSAttributedString *)attributedTitle type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler;
8486

8587
- (void)show;

SIAlertView/SIAlertView.m

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -193,38 +193,51 @@ + (void)initialize
193193
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
194194
paragraphStyle.lineHeightMultiple = 1.1;
195195
paragraphStyle.alignment = NSTextAlignmentCenter;
196-
appearance.titleAttributes = @{NSFontAttributeName : titleFont, NSParagraphStyleAttributeName : paragraphStyle};
197-
appearance.messageAttributes = @{NSFontAttributeName : messageFont, NSParagraphStyleAttributeName : paragraphStyle};
196+
appearance.titleAttributes = @{NSFontAttributeName : titleFont, NSForegroundColorAttributeName : [UIColor blackColor], NSParagraphStyleAttributeName : paragraphStyle};
197+
appearance.messageAttributes = @{NSFontAttributeName : messageFont, NSForegroundColorAttributeName : [UIColor darkGrayColor], NSParagraphStyleAttributeName : paragraphStyle};
198198

199199
UIFont *defaultButtonFont = [UIFont systemFontOfSize:[UIFont buttonFontSize]];
200200
UIFont *otherButtonFont = [UIFont boldSystemFontOfSize:[UIFont buttonFontSize]];
201-
appearance.defaultButtonAttributes = @{NSFontAttributeName : defaultButtonFont};
202-
appearance.cancelButtonAttributes = @{NSFontAttributeName : otherButtonFont};
201+
appearance.defaultButtonAttributes = @{NSFontAttributeName : defaultButtonFont, NSForegroundColorAttributeName : [UIColor blackColor]};
202+
appearance.cancelButtonAttributes = @{NSFontAttributeName : otherButtonFont, NSForegroundColorAttributeName : [UIColor blackColor]};
203203
appearance.destructiveButtonAttributes = @{NSFontAttributeName : otherButtonFont, NSForegroundColorAttributeName : [UIColor colorWithRed:0.96f green:0.37f blue:0.31f alpha:1.00f]};
204204
}
205205

206206
- (id)initWithTitle:(NSString *)title message:(NSString *)message
207207
{
208208
self = [super init];
209209
if (self) {
210-
if (title) {
211-
_attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:[self titleAttributes]];
212-
}
213-
if (message) {
214-
_attributedMessage = [[NSAttributedString alloc] initWithString:message attributes:[self messageAttributes]];
215-
}
210+
self.title = title;
211+
self.message = message;
216212

217213
self.items = [NSMutableArray array];
218214
}
219215
return self;
220216
}
221217

218+
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButton:(NSString *)canelButton handler:(SIAlertViewHandler)handler
219+
{
220+
self = [super init];
221+
if (self) {
222+
self.title = title;
223+
self.message = message;
224+
225+
self.items = [NSMutableArray array];
226+
227+
if (canelButton) {
228+
[self addButtonWithTitle:canelButton type:SIAlertViewButtonTypeCancel handler:handler];
229+
}
230+
}
231+
return self;
232+
}
233+
222234
- (id)initWithAttributedTitle:(NSAttributedString *)attributedTitle attributedMessage:(NSAttributedString *)attributedMessage
223235
{
224236
self = [super init];
225237
if (self) {
226-
_attributedTitle = [attributedTitle copy];
227-
_attributedMessage = [attributedMessage copy];
238+
self.attributedTitle = attributedTitle;
239+
self.attributedMessage = attributedMessage;
240+
228241
self.items = [NSMutableArray array];
229242
}
230243
return self;
@@ -343,19 +356,31 @@ - (void)setAttributedMessage:(NSAttributedString *)attributedMessage
343356

344357
- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
345358
{
346-
NSDictionary *attributes = nil;
359+
[self addButtonWithTitle:title font:nil color:nil type:type handler:handler];
360+
}
361+
362+
- (void)addButtonWithTitle:(NSString *)title font:(UIFont *)font color:(UIColor *)color type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
363+
{
364+
NSDictionary *defaults = nil;
347365
switch (type) {
348366
case SIAlertViewButtonTypeDefault:
349-
attributes = self.defaultButtonAttributes;
367+
defaults = self.defaultButtonAttributes;
350368
break;
351369
case SIAlertViewButtonTypeCancel:
352-
attributes = self.cancelButtonAttributes;
370+
defaults = self.cancelButtonAttributes;
353371
break;
354372
case SIAlertViewButtonTypeDestructive:
355-
attributes = self.destructiveButtonAttributes;
373+
defaults = self.destructiveButtonAttributes;
356374
break;
357375
}
358-
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attributes];
376+
NSMutableDictionary *temp = [defaults mutableCopy];
377+
if (font) {
378+
temp[NSFontAttributeName] = font;
379+
}
380+
if (color) {
381+
temp[NSForegroundColorAttributeName] = color;
382+
}
383+
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:temp];
359384
[self addButtonWithAttributedTitle:attributedTitle type:type handler:handler];
360385
}
361386

@@ -925,6 +950,7 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
925950
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
926951
button.tag = index;
927952
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
953+
928954
[button setAttributedTitle:item.attributedTitle forState:UIControlStateNormal];
929955
UIImage *normalImage = nil;
930956
UIImage *highlightedImage = nil;
@@ -1060,39 +1086,39 @@ - (void)setShadowRadius:(CGFloat)shadowRadius
10601086
- (UIColor *)defaultButtonBackgroundColor
10611087
{
10621088
if (!_defaultButtonBackgroundColor) {
1063-
return [[SIAlertView appearance] defaultButtonBackgroundColor];
1089+
return [[[self class] appearance] defaultButtonBackgroundColor];
10641090
}
10651091
return _defaultButtonBackgroundColor;
10661092
}
10671093

10681094
- (UIColor *)cancelButtonBackgroundColor
10691095
{
10701096
if (!_cancelButtonBackgroundColor) {
1071-
return [[SIAlertView appearance] cancelButtonBackgroundColor];
1097+
return [[[self class] appearance] cancelButtonBackgroundColor];
10721098
}
10731099
return _cancelButtonBackgroundColor;
10741100
}
10751101

10761102
- (UIColor *)destructiveButtonBackgroundColor
10771103
{
10781104
if (!_destructiveButtonBackgroundColor) {
1079-
return [[SIAlertView appearance] destructiveButtonBackgroundColor];
1105+
return [[[self class] appearance] destructiveButtonBackgroundColor];
10801106
}
10811107
return _destructiveButtonBackgroundColor;
10821108
}
10831109

10841110
- (NSDictionary *)titleAttributes
10851111
{
10861112
if (!_titleAttributes) {
1087-
return [[SIAlertView appearance] titleAttributes];
1113+
return [[[self class] appearance] titleAttributes];
10881114
}
10891115
return _titleAttributes;
10901116
}
10911117

10921118
- (NSDictionary *)messageAttributes
10931119
{
10941120
if (!_messageAttributes) {
1095-
return [[SIAlertView appearance] messageAttributes];
1121+
return [[[self class] appearance] messageAttributes];
10961122
}
10971123
return _messageAttributes;
10981124
}
@@ -1101,36 +1127,25 @@ - (NSDictionary *)defaultButtonAttributes
11011127
{
11021128
NSDictionary *attributes = _defaultButtonAttributes;
11031129
if (!attributes) {
1104-
attributes = [[SIAlertView appearance] defaultButtonAttributes];
1130+
attributes = [[[self class] appearance] defaultButtonAttributes];
11051131
}
1106-
return [self tintedAttributes:attributes];
1132+
return attributes;
11071133
}
11081134

11091135
- (NSDictionary *)cancelButtonAttributes
11101136
{
11111137
NSDictionary *attributes = _cancelButtonAttributes;
11121138
if (!attributes) {
1113-
attributes = [[SIAlertView appearance] cancelButtonAttributes];
1139+
attributes = [[[self class] appearance] cancelButtonAttributes];
11141140
}
1115-
return [self tintedAttributes:attributes];
1141+
return attributes;
11161142
}
11171143

11181144
- (NSDictionary *)destructiveButtonAttributes
11191145
{
11201146
NSDictionary *attributes = _destructiveButtonAttributes;
11211147
if (!attributes) {
1122-
attributes = [[SIAlertView appearance] destructiveButtonAttributes];
1123-
}
1124-
return [self tintedAttributes:attributes];
1125-
}
1126-
1127-
// support tint
1128-
- (NSDictionary *)tintedAttributes:(NSDictionary *)attributes
1129-
{
1130-
if (!attributes[NSForegroundColorAttributeName]) {
1131-
NSMutableDictionary *temp = [attributes mutableCopy];
1132-
temp[NSForegroundColorAttributeName] = self.tintColor;
1133-
attributes = [temp copy];
1148+
attributes = [[[self class] appearance] destructiveButtonAttributes];
11341149
}
11351150
return attributes;
11361151
}

0 commit comments

Comments
 (0)