diff --git a/SIAlertView/SIAlertView.h b/SIAlertView/SIAlertView.h index e4d620a..1d1a7c5 100644 --- a/SIAlertView/SIAlertView.h +++ b/SIAlertView/SIAlertView.h @@ -44,6 +44,7 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *message; +@property (nonatomic, copy) UIImage *icon; @property (nonatomic, assign) SIAlertViewTransitionStyle transitionStyle; // default is SIAlertViewTransitionStyleSlideFromBottom @property (nonatomic, assign) SIAlertViewBackgroundStyle backgroundStyle; // default is SIAlertViewBackgroundStyleGradient @@ -67,6 +68,9 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, strong) UIColor *buttonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *cancelButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *destructiveButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; +@property (nonatomic, strong) UIColor *buttonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; +@property (nonatomic, strong) UIColor *cancelButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; +@property (nonatomic, strong) UIColor *destructiveButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) CGFloat cornerRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 2.0 @property (nonatomic, assign) CGFloat shadowRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 8.0 diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 7a67df3..7635716 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -49,6 +49,7 @@ @interface SIAlertView () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *messageLabel; +@property (nonatomic, strong) UIImageView *iconImageView; @property (nonatomic, strong) UIView *containerView; @property (nonatomic, strong) NSMutableArray *buttons; @@ -348,6 +349,12 @@ - (void)setMessage:(NSString *)message [self invalidateLayout]; } +- (void)setIcon:(UIImage *)icon +{ + _icon = icon; + [self invalidateLayout]; +} + #pragma mark - Public - (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler @@ -731,7 +738,16 @@ - (void)validateLayout self.containerView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.containerView.bounds cornerRadius:self.containerView.layer.cornerRadius].CGPath; CGFloat y = CONTENT_PADDING_TOP; + if (self.iconImageView) { + self.iconImageView.image = self.icon; + CGFloat height = self.icon.size.height; + self.iconImageView.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height); + y += height; + } if (self.titleLabel) { + if (y > CONTENT_PADDING_TOP) { + y += GAP; + } self.titleLabel.text = self.title; CGFloat height = [self heightForTitleLabel]; self.titleLabel.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height); @@ -776,7 +792,13 @@ - (void)validateLayout - (CGFloat)preferredHeight { CGFloat height = CONTENT_PADDING_TOP; + if (self.icon) { + height += self.icon.size.height; + } if (self.title) { + if (height > CONTENT_PADDING_TOP) { + height += GAP; + } height += [self heightForTitleLabel]; } if (self.message) { @@ -877,6 +899,7 @@ - (void)setup [self setupContainerView]; [self updateTitleLabel]; [self updateMessageLabel]; + [self updateIconImageView]; [self setupButtons]; [self invalidateLayout]; } @@ -885,6 +908,7 @@ - (void)teardown { [self.containerView removeFromSuperview]; self.containerView = nil; + self.iconImageView = nil; self.titleLabel = nil; self.messageLabel = nil; [self.buttons removeAllObjects]; @@ -904,6 +928,25 @@ - (void)setupContainerView [self addSubview:self.containerView]; } +- (void)updateIconImageView +{ + if (self.icon) { + if (!self.iconImageView) { + self.iconImageView = [[UIImageView alloc] initWithFrame:self.bounds]; + [self.iconImageView setContentMode:UIViewContentModeScaleAspectFit]; + [self.containerView addSubview:self.iconImageView]; +#if DEBUG_LAYOUT + self.titleLabel.backgroundColor = [UIColor redColor]; +#endif + } + self.iconImageView.image = self.icon; + } else { + [self.iconImageView removeFromSuperview]; + self.iconImageView = nil; + } + [self invalidateLayout]; +} + - (void)updateTitleLabel { if (self.title) { @@ -1135,6 +1178,32 @@ - (void)setDestructiveButtonColor:(UIColor *)buttonColor [self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeDestructive]; } +- (void)setButtonBackgroundColor:(UIColor *)backgroundColor +{ + if (_buttonBackgroundColor == backgroundColor) { + return; + } + _buttonBackgroundColor = backgroundColor; + [self setBackgroundColor:backgroundColor toButtonsOfType:SIAlertViewButtonTypeDefault]; +} + +- (void)setCancelButtonBackgroundColor:(UIColor *)backgroundColor +{ + if (_cancelButtonBackgroundColor == backgroundColor) { + return; + } + _cancelButtonBackgroundColor = backgroundColor; + [self setBackgroundColor:backgroundColor toButtonsOfType:SIAlertViewButtonTypeCancel]; +} + +- (void)setDestructiveButtonBackgroundColor:(UIColor *)backgroundColor +{ + if (_destructiveButtonBackgroundColor == backgroundColor) { + return; + } + _destructiveButtonBackgroundColor = backgroundColor; + [self setBackgroundColor:backgroundColor toButtonsOfType:SIAlertViewButtonTypeDestructive]; +} - (void)setDefaultButtonImage:(UIImage *)defaultButtonImage forState:(UIControlState)state { @@ -1179,6 +1248,16 @@ -(void)setColor:(UIColor *)color toButtonsOfType:(SIAlertViewButtonType)type { } } +-(void)setBackgroundColor:(UIColor *)color toButtonsOfType:(SIAlertViewButtonType)type { + for (NSUInteger i = 0; i < self.items.count; i++) { + SIAlertItem *item = self.items[i]; + if(item.type == type) { + UIButton *button = self.buttons[i]; + button.backgroundColor = color; + } + } +} + # pragma mark - # pragma mark Enable parallax effect (iOS7 only)