Skip to content

Commit f7b02bc

Browse files
committed
rewrite setter and getters to support changing theme on per instance bias.
1 parent 01e7fbb commit f7b02bc

1 file changed

Lines changed: 75 additions & 14 deletions

File tree

SIAlertView/SIAlertView.m

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ - (void)drawRect:(CGRect)rect
128128

129129
@interface SIAlertItem : NSObject
130130

131+
@property (nonatomic, copy) NSString *title;
131132
@property (nonatomic, copy) NSAttributedString *attributedTitle;
132133
@property (nonatomic, assign) SIAlertViewButtonType type;
133134
@property (nonatomic, copy) SIAlertViewHandler action;
@@ -174,6 +175,9 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie
174175

175176
@implementation SIAlertView
176177

178+
@synthesize title = _title, message = _message;
179+
@synthesize attributedTitle = _attributedTitle, attributedMessage = _attributedMessage;
180+
177181
+ (void)initialize
178182
{
179183
if (self != [SIAlertView class])
@@ -308,59 +312,109 @@ + (void)hideBackgroundAnimated:(BOOL)animated
308312

309313
- (void)setTitle:(NSString *)title
310314
{
311-
if (title) {
312-
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:[self titleAttributes]];
313-
[self setAttributedTitle:attributedTitle];
314-
} else {
315-
[self setAttributedTitle:nil];
315+
if ([_title isEqualToString:title]) {
316+
return;
317+
}
318+
319+
_title = [title copy];
320+
_attributedTitle = nil;
321+
if (self.isVisible) {
322+
[self updateTitleLabel];
316323
}
317324
}
318325

319326
- (void)setMessage:(NSString *)message
320327
{
321-
if (message) {
322-
NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:message attributes:[self messageAttributes]];
323-
[self setAttributedMessage:attributedMessage];
324-
} else {
325-
[self setAttributedMessage:nil];
328+
if ([_message isEqualToString:message]) {
329+
return;
330+
}
331+
332+
_message = [message copy];
333+
_attributedMessage = nil;
334+
if (self.isVisible) {
335+
[self updateTitleLabel];
326336
}
327337
}
328338

329339
- (NSString *)title
330340
{
331-
return self.attributedTitle.string;
341+
if (!_title) {
342+
return _attributedTitle.string;
343+
}
344+
return _title;
332345
}
333346

334347
- (NSString *)message
335348
{
336-
return self.attributedMessage.string;
349+
if (!_message) {
350+
return _attributedMessage.string;
351+
}
352+
return _message;
337353
}
338354

339355
- (void)setAttributedTitle:(NSAttributedString *)attributedTitle
340356
{
357+
if (_attributedTitle == attributedTitle) {
358+
return;
359+
}
360+
341361
_attributedTitle = [attributedTitle copy];
362+
_title = nil;
342363
if (self.isVisible) {
343364
[self updateTitleLabel];
344365
}
345366
}
346367

347368
- (void)setAttributedMessage:(NSAttributedString *)attributedMessage
348369
{
370+
if (_attributedMessage == attributedMessage) {
371+
return;
372+
}
373+
349374
_attributedMessage = [attributedMessage copy];
375+
_message = nil;
350376
if (self.isVisible) {
351377
[self updateMessageLabel];
352378
}
353379
}
354380

381+
- (NSAttributedString *)attributedTitle
382+
{
383+
if (_attributedTitle) {
384+
return _attributedTitle;
385+
}
386+
if (_title) {
387+
return [[NSAttributedString alloc] initWithString:_title attributes:[self titleAttributes]];
388+
}
389+
return nil;
390+
}
391+
392+
- (NSAttributedString *)attributedMessage
393+
{
394+
if (_attributedMessage) {
395+
return _attributedMessage;
396+
}
397+
if (_message) {
398+
return [[NSAttributedString alloc] initWithString:_message attributes:[self messageAttributes]];
399+
}
400+
return nil;
401+
}
402+
355403
#pragma mark - Public
356404

357405
- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
358406
{
359-
[self addButtonWithTitle:title font:nil color:nil type:type handler:handler];
407+
NSAssert(title != nil, @"Title can't be nil");
408+
SIAlertItem *item = [[SIAlertItem alloc] init];
409+
item.title = title;
410+
item.type = type;
411+
item.action = handler;
412+
[self.items addObject:item];
360413
}
361414

362415
- (void)addButtonWithTitle:(NSString *)title font:(UIFont *)font color:(UIColor *)color type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
363416
{
417+
NSAssert(title != nil, @"Title can't be nil");
364418
NSDictionary *defaults = nil;
365419
switch (type) {
366420
case SIAlertViewButtonTypeDefault:
@@ -386,6 +440,7 @@ - (void)addButtonWithTitle:(NSString *)title font:(UIFont *)font color:(UIColor
386440

387441
- (void)addButtonWithAttributedTitle:(NSAttributedString *)attributedTitle type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
388442
{
443+
NSAssert(attributedTitle != nil, @"Attributed title can't be nil");
389444
SIAlertItem *item = [[SIAlertItem alloc] init];
390445
item.attributedTitle = attributedTitle;
391446
item.type = type;
@@ -951,34 +1006,40 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
9511006
button.tag = index;
9521007
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
9531008

954-
[button setAttributedTitle:item.attributedTitle forState:UIControlStateNormal];
1009+
NSDictionary *defaults = nil;
9551010
UIImage *normalImage = nil;
9561011
UIImage *highlightedImage = nil;
9571012
switch (item.type) {
9581013
case SIAlertViewButtonTypeCancel:
9591014
if (self.cancelButtonBackgroundColor) {
9601015
normalImage = [self imageWithUIColor:self.cancelButtonBackgroundColor];
9611016
highlightedImage = [self imageWithUIColor:[self highlightedColorWithColor:self.cancelButtonBackgroundColor]];
1017+
defaults = self.cancelButtonAttributes;
9621018
}
9631019
break;
9641020
case SIAlertViewButtonTypeDestructive:
9651021
if (self.destructiveButtonBackgroundColor) {
9661022
normalImage = [self imageWithUIColor:self.destructiveButtonBackgroundColor];
9671023
highlightedImage = [self imageWithUIColor:[self highlightedColorWithColor:self.destructiveButtonBackgroundColor]];
1024+
defaults = self.destructiveButtonAttributes;
9681025
}
9691026
break;
9701027
case SIAlertViewButtonTypeDefault:
9711028
default:
9721029
if (self.defaultButtonBackgroundColor) {
9731030
normalImage = [self imageWithUIColor:self.defaultButtonBackgroundColor];
9741031
highlightedImage = [self imageWithUIColor:[self highlightedColorWithColor:self.defaultButtonBackgroundColor]];
1032+
defaults = self.defaultButtonAttributes;
9751033
}
9761034
break;
9771035
}
9781036
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
9791037
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
9801038
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
9811039

1040+
NSAttributedString *title = item.attributedTitle ? item.attributedTitle : [[NSAttributedString alloc] initWithString:item.title attributes:defaults];
1041+
[button setAttributedTitle:title forState:UIControlStateNormal];
1042+
9821043
return button;
9831044
}
9841045

0 commit comments

Comments
 (0)